Facade pattern
definition
Provides a unified interface, the interface used to access a group subsystem. The appearance of a high-level interface that hides the complexity of the system to the client provides an interface can access the system, so that the subsystem easier to use.
structure
SubSystem *: subsystem interface can be invoked within them by a certain order to access the data resource.
Facade: This class provides the appearance of a common external interface subsystem.
Client: client object interface to read data resource each interface subsystem through a look.
application
API Gateway is the enterprise IT to provide external access to the internal interface services on the system boundary unified entrance.
API gateway is a server, only the inlet system. From the perspective of object-oriented design, it is similar to the appearance model. In the micro-service architecture, API Gateway entry all services are commonly used for user authentication and authorization requests and forwarding route.
API Getway like appearance mode Facade, each service is SubSystem *. According to the request sent by the Client, routed to a service.
public interface Service {
void server();
}
public class ServiceA implements Service {
@Override
public void server() {
System.out.println("Request reached SERVICE-A!");
//...复杂的方法调用
}
}
public class ServiceB implements Service {
@Override
public void server() {
System.out.println("Request reached SERVICE-B!");
//...复杂的方法调用
}
}
public class ServiceC implements Service {
@Override
public void server() {
System.out.println("Request reached SERVICE-C!");
//...复杂的方法调用
}
}
public class APIGetway {
private Map<String, Service> services = new HashMap<>(8);
public void addService(String name, Service service) {
services.put(name, service);
}
public void forward(String request) throws IOException {
if (services.containsKey(request)) {
services.get(request).server();
} else {
throw new IOException("No Such Service!");
}
}
}
public class Client {
private APIGetway apiGetway;
public Client(APIGetway apiGetway) {
this.apiGetway = apiGetway;
}
public void sendRequest(String request){
try {
apiGetway.forward(request);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Test categories:
public class Main {
public static void main(String[] args) {
APIGetway apiGetway = new APIGetway();
apiGetway.addService("SERVICE-A",new ServiceA());
apiGetway.addService("SERVICE-B",new ServiceB());
apiGetway.addService("SERVICE-C",new ServiceC());
Client client = new Client(apiGetway);
client.sendRequest("SERVICE-A");
client.sendRequest("SERVICE-B");
client.sendRequest("SERVICE-C");
}
}
Request reached SERVICE-A!
Request reached SERVICE-B!
Request reached SERVICE-B!
to sum up
The real subsystem interface is not as simple as I have demonstrated, if not through a unified interface to provide the appearance of pattern, then finished a particular specific business needs can be complicated.
Do not use the appearance of the completion of a business model:
SubSystem1.mothod1();
SubSystem2.mothod2();
SubSystem3.mothod3();
SubSystem4.mothod4();
//...
Use skin mode:
facade.service();
Thus the appearance of a complete pattern may be packaged business process, so that fine-grained control not exposed to service the caller.
advantage
-
To achieve a loose coupling relationship between the subsystem and the client.
-
The client subsystem shield assembly, reduces the number of objects required by the client process, and easier to use such subsystems.
Shortcoming
- Mode is only completed by the appearance of a particular service, the operation is difficult to achieve certain fine-grained.
Scenarios
-
Module provides access to the outside world as complex modules or subsystems;
-
Chromatographic structure, each layer may be used to define the system mode of appearance of the inlet.