Java Design pattern Appearance pattern Learning notes _java

Source: Internet
Author: User

Appearance mode: Also known as façade mode: Facade façade provides a consistent interface for a set of interfaces of a subsystem, making this subsystem easy to use (by introducing a new appearance role to reduce the complexity of the original system, while reducing the coupling between the customer class and the subsystem).

Image source: Design pattern: The basis of reusable object-oriented software.

Realize

Case demand: Rent

Have to find a room to rent their own experience to find a room is a very painful thing to do, not only to run the neighborhood and also with (ii) the landlord bargain. And then he learned to be smart, no longer their own door, but directly to find like chain home, I love my house such a housing intermediary, they hold a certain amount of property, we just pay them a commission, they can bargain for our landlord, and they are mostly very professional, save time and save money. At this time the housing intermediary is a façade, and housing rental households is the subsystem subsystem:

Façade
appearance class: Know which subsystems are responsible for processing requests, and delegate the client's request to the appropriate subsystem object:

 public class Mediumfacade {private Cuiyuanapartment Cuiyuan;

  Private Xixiapartment Xixi;

  Private Xihuapartment Xihu;
    Public Mediumfacade () {Cuiyuan = new Cuiyuanapartment ("Chui community", 900, 1);
    Xixi = new Xixiapartment ("Xixi garden", 1200, 1);
  Xihu = new Xihuapartment ("West Lake community", 2600, 1); The public void Rentinghouse (double) {//The price is appropriate and has a room to group if (Prices >= cuiyuan.getprice () && Cuiyuan.
      GetStatus ()!= 0) {System.out.println ("reservation" + cuiyuan.getlocation ());
    Cuiyuan.setstatus (0); else if Price >= xixi.getprice () && xixi.getstatus ()!= 0) {System.out.println ("booking" + xixi.getlocatio
      n ());
    Xixi.setstatus (0); else if Price >= xihu.getprice () && xihu.getstatus ()!= 0) {System.out.println ("booking" + xihu.getlocatio
      n ());
    Xihu.setstatus (0);
    else {System.out.println ("Bid too low/No listings ..."); }
  }
}

subsystem 
  Subsystem Collection: Implements subsystem functionality to handle tasks assigned by a façade object (note that there is no façade information in the subsystem. That is, there are no façade object references):

/** * @author Jifang * @since 16/8/23 10:12.

  * * Public class Xihuapartment {private String location;

  private double price;

  private int status;
    Public Xihuapartment (String location, double price, int status) {this.location = location;
    This.price = Price;
  This.status = status;
  Public String getLocation () {return location;
  Public double GetPrice () {return price;
  public int GetStatus () {return status;
  public void setstatus (int status) {this.status = status;

  } class Xixiapartment {private String location;

  private double price;

  private int status;
    Public Xixiapartment (String location, double price, int status) {this.location = location;
    This.price = Price;
  This.status = status;
  Public String getLocation () {return location;
  Public double GetPrice () {return price;
  public int GetStatus () {return status; public void setstatus (int status) {THis.status = status;

  } class Cuiyuanapartment {private String location;

  private double price;

  private int status;
    Public Cuiyuanapartment (String location, double price, int status) {this.location = location;
    This.price = Price;
  This.status = status;
  Public String getLocation () {return location;
  Public double GetPrice () {return price;
  public int GetStatus () {return status;
  public void setstatus (int status) {this.status = status;
 }
}

Client
In this way, the client only needs to contact a housing intermediary and give us a quote, they will help us contact all the landlord:

public class Client {

  @Test the public
  Void client () {
    Mediumfacade façade = new Mediumfacade ();
    Facade.rentinghouse (a);
  }


Summary

Students with object-oriented development experience, even if they have not heard of the appearance of the model, it is entirely possible to use him, because he perfectly embodies the principle of relying on the inverted and Dimitri principles of the idea, is a very common pattern.

Use


First of all, in the early stage of design, should be aware of the level of separation, such as the classic three-tier architecture, layer and layer to create a façade, which can provide a simple interface for complex subsystems, so that the coupling greatly reduced.

Second, in the development phase, subsystems tend to become more and more complex because of continuous refactoring, and adding a façade can provide a simple interface to reduce dependencies between modules.

Third, when maintaining a legacy system, this system may be very difficult to maintain and expand, but because it contains very important features, new requirements must rely on it, you can develop a façade for the new system, for the design of rough or highly complex legacy code to provide a relatively clear and simple interface, Let the new system interact with the façade, and the façade interacts with the legacy code for all the tedious work.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.