|
Design Pattern |
|
Benefits:
• Provides simple interface to a complex system without reducing options provided by the system.
• Increases the reuse of classes by decoupling the interface from the implementation
• Promotes weak coupling between the subsystem and its clients
• Translates client requests to subsystem that can fulfill request
Decorators and Facade evoke similar images in building architecture, but in design pattern
terminology, the Facade is a way of hiding a complex system inside a simpler interface,
whereas Decorator adds function by wrapping a class.
The UML diagram for this pattern is something like this:
This UML diagram shows that, there can be any number of classes involved in this “Facade”
system. There is one client, one facade, and multiple
classes underneath the facade. In a typical situation, the facade would have a limited amount of actual code, making calls to lower layers most of the time. |
|
We can understand the facade pattern better using a simple example. Let’s consider a Bank.
This bank has a service provider. In the bank, there are a lot of accounts available e.g. saving A/c, current A/c material, corporate A/c. You, as a client want to access your different accounts. You just have access to service provider, who knows well about your accounts information to update inquiries regarding your account. Whatever you want, you ask the service provider and he retrieves it out and tells you on showing him the credentials.
Here, the service provider acts as the facade, as he hides the complexities of the
Bank information system.
Let us see how the Bank example works.
Bank.java
public interface Bank {
public Inquiries getInquiry(String
inquiryType); }// End of interface
}// End of interface
The Bank is an interface that only returns Inquiries. The Inquiries are of three types as
discussed before:SavingInquiries, CurrentInquiries and CorporatetInquiries.
All these classes can implement the Inquiries interface. Let’s have a look at the code for one of the Inquiry.
SavingInquiryBank.java
public class SavingInquiryBank implements
Bank { public Inquiries getInquiry ( ) {//
write functionality of saving
accountSavingInquiries savinginquiries = new
SavingInquiries();return savinginquiries;} }//
End of class
Now let’s consider the facade ServiceProvider.
|
|
Nov 2007 | Java Jazz Up | 39 |
|
|
|
View All Topics |
All Pages of this Issue |
Pages:
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
49,
50,
51,
52,
53 ,
54,
55,
56,
57,
58,
59,
60,
61,
62,
63 ,
64,
65 ,
66 ,
67 ,
68 ,
69 Download PDF |
|
|
|
|
|
|
|
|
|