[Appearance Mode Application Scenario example]
For example, if you are playing the "need for speed" game, you only need to wait for a countdown to 0 and rush to the first place at the fastest speed, however, gamers have no idea what to do before they rush out, such as downgrading, clutch, fuel tank detection, and adjusting the direction, encapsulate these micro-operations into an interface, that is, the appearance mode. In web development, the MVC layered architecture is a typical appearance mode. Each layer hides the specific content of the operation and retains an interface for the upper layer to call.
[Appearance mode description]
Type: Structure Mode
Provides a consistent interface for a group of interfaces in the subsystem.
[Appearance mode UML diagram]
[Appearance mode-Java code implementation]
Create a racing car:
Package car_package;
Public class car {
Public void start (){
System. Out. println ("car started ");
}
Public void check_stop (){
System. Out. println ("Brake check ");
}
Public void check_box (){
System. Out. println ("Check tank ");
}
Public void check_console (){
System. Out. println ("check whether the dashboard is abnormal ");
}
} |
Appearance of the new racing car operation:
Package car_facade;
Import car_package.car;
Public class car_facade_imple {
Public void car_go_go (CAR car_ref ){
Car_ref.check_box ();
Car_ref.check_console ();
Car_ref.check_stop ();
Car_ref.start ();
}
} |
Create a client running class:
Package run_main;
Import car_facade.car_facade_imple; Import car_package.car;
Public class run_main {
Public static void main (string [] ARGs ){
Car_facade_imple car_facade_imple_ref = new car_facade_imple ();
Car_facade_imple_ref.car_go_go (new car ());
}
} |
The program running result is as follows:
Check fuel tank
Check whether the dashboard is abnormal.
Brake check
Car started |
It's easy. encapsulate a sub-operation with an external interface, and then call this interface to call those very complex micro-operations.
[Appearance mode-Java code implementation of myeclipse6 project]
Sample facade code: Click