23 Design Patterns in Java 6-appearance mode (facade pattern)

Source: Internet
Author: User

Concept: Provides a unified interface for a set of interfaces in a subsystem. The facade mode defines a high-level interface that makes the subsystem easier to use.

Application Scenarios:

1) provides a simple interface for a complex subsystem.

2) Improve the independence of the subsystem.

3) In a hierarchical structure, you can use the facade pattern to define the entry for each layer in the system.

Take the leisure one-stop service offered by the large entertainment mall as an example, including shopping, dining, massage. Which shopping, massage, catering as a subsystem of the composition, providing a unified external interface Packageservice

The class diagram is as follows:

Class Shopping
{
String Customer=null;
Public shopping (String customer)
{
This.customer=customer;
}

public void Doservice ()
{
SYSTEM.OUT.PRINTLN ("Shopping service for" +customer);
}
}

Class Meal
{
String Customer=null;
Public Meal (String customer)
{
This.customer=customer;
}

public void Doservice ()
{
SYSTEM.OUT.PRINTLN ("Meal Service for" +customer);
}
}

Class Massage
{
String Customer=null;
Public Massage (String customer)
{
This.customer=customer;
}

public void Doservice ()
{
SYSTEM.OUT.PRINTLN ("Massage service for" +customer);
}
}
Class Packageservice
{
String Customer=null;
Shopping ashopping=null;
Meal Ameal=null;
Massage amassage=null;
Public Packageservice (String customer)
{
This.customer=customer;
Ashopping=new shopping (customer);
Ameal=new Meal (customer);
Amassage=new Massage (customer);
}
public void Doservice ()
{
Ashopping.doservice ();
Ameal.doservice ();
Amassage.doservice ();
}
}
public class Facadepatterntest
{
public static void Main (string[] args)
{
String customer= "Wudy";
Shopping Ashopping=new Shopping (customer);
Ashopping.doservice ();
Meal ameal=new Meal (customer);
Ameal.doservice ();
Massage amassage=new Massage (customer);
Amassage.doservice ();
//----------------------
SYSTEM.OUT.PRINTLN ("------Now was use facade pattern-------");
Packageservice apackageservice=new Packageservice (customer);
Apackageservice.doservice ();
}
}

////////////////////////////////////////

Output Result:

Shopping service for Wudy
Meal Service for Wudy
Massage service for Wudy
------Now are use facade pattern-------
Shopping service for Wudy
Meal Service for Wudy
Massage service for Wudy

23 Design Patterns in Java 6-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.