1. The contents of this section
- Introduction to the intent of the façade mode
- The benefits of Façade mode
- Application Scenarios for Façade mode
- 3 ways to implement the pattern: POJO, stateless and stateful reply bean façade
- Important differences between stateful and stateless bean facades
- Warnings about the use of façade mode and potential pitfalls
2. Introduction to Façade mode
In Gof, this describes the façade pattern: "Provides a unified interface for a set of interfaces in a subsystem." Head first Design patterns gives the same explanation, but also points out that while hiding subsystem complexity, façade mode provides the full functionality of the subsystem through an easy-to-use interface. Its basic class diagram 2-1
Figure 2-1 Façade pattern class diagram
3. Benefits of Façade mode
(1) Reduced coupling because the client has no knowledge of the subsystem.
(2) Enhanced maintainability and manageability in the case of changes.
(3) Implement functional reuse, because façade mode encourages control reuse and fine-grained logic.
(4) The same method is called each time the method is called, ensuring consistency of service execution.
(5) Grouping related methods and invoking them from a method call to reduce the complexity of the business logic.
(6) Centralized security and transaction control management.
(7) Testability and the mode of simulation can be realized.
4. Application Scenarios for Façade mode
(1) provides simple and unified access to legacy backend systems.
(2) Create a public API for the class, such as a driver.
(3) Provide coarse-grained access to available services.
(4) Reduce network calls. The façade initiates multiple calls to the subsystem, and the remote client makes only one call to the façade.
(5) Encapsulate the application process with internal details to improve security and simplify operations.
5. Implement the façade mode
5.1 Pojo Way Realization
Public classWashingmachine {/*** The function that the washing machine presents to the user is * 1. Strong washing 2. Light washing * Then the user in the operation only need to set is strong washing or mild washing, do not take care of water temperature, speed, etc. * fine-grained function * This is a typical façade mode */ Public voidheavilysoiled () {setwatertemperature (100); Setwashcycleduration (90); Setspincycleduration (10); Adddetergent (); Addbleach (); Addfabricsoftener (); Heatwater (); Startwash (); } Public voidlightlysoiled () {setwatertemperature (40); Setwashcycleduration (20); Setspincycleduration (10); Adddetergent (); Heatwater (); Startwash (); } Private voidStartwash () {}Private voidHeatwater () {}Private voidAddfabricsoftener () {}Private voidAddbleach () {}Private voidadddetergent () {}Private voidSetspincycleduration (inti) {}Private voidSetwashcycleduration (inti) {}Private voidSetwatertemperature (inti) {}}
5.2 Façade mode for stateless beans
@Stateless public class CustomerService { public< /span> long getcustomer (int SessionID) { return 100005l ; public boolean checkid (long x) { return
true ; }}
@Stateless Public class Loanservice { publicboolean checkcreditrating (long ID,double Amount) { // Check if customer is eligible for the amount return True; } }
Public classAccountservice { Public BooleanGetloan (Doubleamount) { //Check if bank value has enough return true; } Public BooleanSetcustomerbalance (LongIdDoubleamount) { //set new customer balance return true; }}
@Stateless Public classBankservicefacade {@Inject customerservice customerservice; @Inject Loanservice Loanservice; @Inject Accountservice Accountservice; Public BooleanGetloan (intSessionId,Doubleamount) { Booleanresult =false; LongID =Customerservice.getcustomer (sessionId); if(Customerservice.checkid (id)) {if(loanservice.checkcreditrating (ID, amount)) {if(Accountservice.getloan (amount)) {result=accountservice.setcustomerbalance (ID, amount); } } } returnresult; }}
The above is the application of façade pattern in Java EE, note two annotations
@Stateless: Indicates a stateless session Bean
@Inject: Indicates dependency injection
From the above code, it can be seen that a façade can invoke the façade in other subsystems, which in turn encapsulates their own logic and process. This demonstrates one of the benefits of using facades: simplifying the method invocation hierarchy.
5.3 Stateful bean implementation façade
Implement the façade with the state bean basically the same as the stateless bean implementation façade, the only difference is to change the @stateless annotation to a @stateful annotation, which marks the bean as stateful.
6 warnings and potential threats about using the façade mode
Façade mode should be applied with high-level encapsulation complex (business) logic, and provide neat access to a single point through an API.
If you want to provide an interface or API to others, first consider the complexity of the logic and the changes that may occur. Façade mode has the advantage of providing a clean API while hiding parts of the subject that may change.
However, it is not a good practice to wrap a method into the façade unnecessarily, and it will add an unnecessary level. Improper encapsulation can lead to too many calls, as well as a level of worthless value.
When implementing a session façade, it is necessary to determine whether the use case needs to maintain state, only one method of calling the façade to accept the required service's use case is non-conversational, so there is no need to maintain session state between method calls, on the other hand, if you must maintain session state between the method calls, The most appropriate way is to implement this façade as a stateful session bean.
It is important to note that when a stateful session façade is used, he consumes the server resources until the client triggers the session to release it or time out. This means that for most of the time, a session bean façade is bound to the client, but nothing is done, it only maintains state and uses resources. Unlike a stateless session bean façade, it cannot be reused and shared among other clients because each request creates a new instance of the stateless façade and maintains the state of the client session.
7. Summary
The façade mode can be implemented as a Pojo, stateless session Bean, stateful session bean. The various ways of implementing the façade pattern solve different problems under different use case scenarios. However, the implementation does not deviate from its main intent: to provide a high-level, simple interface for complex subsystems.
Be careful when deciding to implement a façade as a stateful session bean, ensuring that it does not cause resource consumption issues.
Well-designed applications take full advantage of façade patterns to encapsulate complex logic and decouple subsystems from clients, but improper use and overuse of façade patterns can lead to more complex, multi-layered systems.
The session façade pattern is similar to the boundary in entity-control-boundary schema mode, and is also related to adapter and wrapper mode.
Java EE design mode--façade mode