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)