Java design pattern-Facade Pattern

Source: Internet
Author: User

For the Facade mode, someone translates it into the appearance mode, and someone translates it into the Facade mode. When I first came into contact with this design model in Hangzhou, the Facade layer of the server had two main functions at that time. One was to provide interfaces for the Flex client. One is a unified interface that contains multiple service operations. GoF "Design Pattern" states that it provides a consistent interface for a group of interfaces in the subsystem. The Facade Pattern Defines a high-level interface, which makes the subsystem easier to use. In fact, most people understand the Facade layer to provide an entrance for multiple complex actions. Let's look at an example that is widely circulated on the Internet: for example, there is a drawer that stores a large and important file. We call it the second drawer, and its key is placed in another drawer, we call it the first drawer. Only the key in the first drawer can be opened. [Java] package com. howlaa. facadeMode;/*** drawer 1 * @ author howlaa */public class DrawerOne {public String open () {System. out. println ("the first drawer opens... "); return getKey ();} public String getKey () {System. out. println ("Get the key for the second drawer... "); return" key ";}} [java] package com. howlaa. facadeMode;/*** drawer 2 * @ author howlaa */public class DrawerTwo {public void open (String key) {if ("key ". equals (key) {System. ou T. println ("the second drawer opens... "); getFile ();} else {System. out. println ("Key mismatch, illegal to open the second drawer... "); return ;}} public void getFile () {System. out. println ("get important files... ") ;}} now let's take a look at what the client wants to get this important file if it does not use the Facade mode: [java] package com. howlaa. facadeMode; import org. junit. test;/*** Client * @ author howlaa **/public class Client {/* ------- when Facade mode is not used --------- */@ Test public void NotUseFacade () {DrawerOne draw ErOne = new DrawerOne (); String key = drawerOne. open (); DrawerTwo drawerTwo = new DrawerTwo (); drawerTwo. open (key);}/* ------- end does not use the Facade mode --------- */}. You must open the first drawer before using this key to open the second drawer. In reality, the user may not know the necessary conditions. He only knows what he needs is to open the drawer and then get the file. Let's take a look at the situation when using the Facade mode: first, we need to create a Facade class: [java] package com. howlaa. facadeMode; public class DrawerFacade {DrawerOne drawerOne = new DrawerOne (); DrawerTwo drawerTwo = new DrawerTwo (); public void open () {String key = drawerOne. open (); drawerTwo. open (key) ;}} then check the client operation: [java] package com. howlaa. facadeMode; import org. junit. test;/*** Client * @ author howlaa **/public class Client {/* -------- use Facade mode -------------- */@ Test public void UseFacade () {DrawerFacade drawerFacade = new DrawerFacade (); drawerFacade. open ();}/* -------- end use Facade mode ----------*/}

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.