[GitHub Repository for Code Samples]
https://github.com/sumithpuri/skp-code-marathon-pattaya
Was going through the book ‘Head First Design Patterns’, came up with my own examples to understand them further. Try downloading the code and see if it helps you in comprehending these in a better way.
Facade Pattern [Sample Code]
Consider a scenario where we require multiple method invocations on various classes, to achieve the desired functionality. Also, consider that this set of functionality is repeatedly being used in your code. If you are thinking of an option where you will perform direct invocations, you are bound to end up with code maintenance issues and tightly coupled code. If these invocations are remote, it is going to be worse with respect to the performance.
Consider a scenario where we require multiple method invocations on various classes, to achieve the desired functionality. Also, consider that this set of functionality is repeatedly being used in your code. If you are thinking of an option where you will perform direct invocations, you are bound to end up with code maintenance issues and tightly coupled code. If these invocations are remote, it is going to be worse with respect to the performance.
Fig. 2 : Facade (Structural) Design Pattern - Class Diagram
[Source : Wikipedia]
Under
the above mentioned conditions is where the facade comes into play.
Herein multiple method invocations are encapsulated into a single method
of the facade class, to achieve the desired functionality. It provides
us with a single point of change and looser coupling, with respect to
the individual implementations. Remote method invocation patterns like
SessionFacade (EJB) adapt from here to improve the overall performance
and lower complexity.
Fig. 3 : Facade (Structural) Design Pattern - Sequence Diagram
[Source : Wikipedia]
The
example attached is a very simple scenario of a InvoiceManagerFacade
which has addInvoice() and deleteInvoice() methods. To achieve the
desired result, each of these methods encapsulates the method
invocations from OrderManager, LedgerManager and BillingManager classes.
AccountsCentral is the main class. Try adding your own method to the facade class, or try plugging in a new type of facade.
AccountsCentral is the main class. Try adding your own method to the facade class, or try plugging in a new type of facade.
No comments:
Post a Comment