Façade?
If it is in accordance with the development model of rup+, with the iteration and increment, the software code will be more and more large, the relationship between the modules will become more and more complex, how you are the first to accept the development of the project is not very difficult, to understand the relationship between them, to continue to complete the following work. But instead of doing this, prepare a "window" for this large program. This way, we don't have to focus on each class individually, just simply request a "window".
Keywords: * * Responsibility relationships, dependencies, call order * *
Key concepts: The provided window can be understood as opening up a high-level interface
Use design mode when declaring:
When a programmer says proudly, "Ah, this class needs to be called before the class can be called." Before calling that method, you need to register it in this class first, which means that we need to introduce the facade role.
Related Design Patterns:
The abstract Factory mode (8th) can consider the abstract Factory pattern as the facade mode when generating complex instances. Because it provides a simple interface that "you want to generate this instance just call this method OK".
Singleton mode (chapter 5th) sometimes creates facade roles using singleton mode.
Mediator Mode (16th) in facade mode, the facade role uses other roles unilaterally to provide high-level interfaces (APIs).
In mediator mode, the mediator role acts as arbiter between colleague roles. It can be said that the facade pattern is unidirectional, while the mediator character is bidirectional.
Understanding Responsibilities
- Objective: To implement a program that reads text into a Web page link.
Package ========> class ======> explanation
PageMaker | Database class that gets the user name from the e-mail address
PageMaker | HTMLWriter class for writing HTML files
PageMaker | PageMaker | Write the user's Web page based on the e-mail address
| no package| Maint class for testing program behavior
Hiding claims in htmlwriter:html requires that the title () be called First and in PageMaker we maintain the order of the calls ahead of time, and the maint does not require a relationship-specific invocation dependency.
Uml
Class Diagram:
Code
public class DataBase { private DataBase(){ } /** * 输入文件名读取数据 * @param filname * @return */ public static Properties getProperties(String filname){ String filename=filname+".ini"; Properties prop = new Properties(); try { prop.load(new FileInputStream(filename)); }catch (IOException e){ e.printStackTrace(); } return prop; }}
public class HTMLWriter {private Writer printwriter; Public HTMLWriter (writer writer) {this.printwriter = writer; }//Output caption (This is a hidden condition must first call the title method) public void title (String title) throws IOException {Printwriter.write ("
public class PageMaker { private PageMaker(){ } /** * 外观接口 * @param mailaddr * @param filename */ public static void makeWelcomePage(String mailaddr,String filename){ try { Properties mailporo = DataBase.getProperties("testmail"); String usernmae = mailporo.getProperty(mailaddr); HtmlWriter htmlWriter = new HtmlWriter(new FileWriter(filename)); htmlWriter.title("欢迎来到"+usernmae+"页面!"); htmlWriter.paragraph(usernmae+"欢迎来到"+usernmae+" 的主页。"); htmlWriter.paragraph("欢迎给我发送邮件:"); htmlWriter.link(mailaddr,usernmae); htmlWriter.close(); System.out.println("这是由于"+usernmae+"创建的邮件的主页界面"+ "From to"+usernmae); }catch (IOException e){ e.printStackTrace(); } }}
public class MainT { public static void main(String[] args) { PageMaker.makeWelcomePage("[email protected]","welcome.html"); }}
Appearance mode (facade)