Magazine
 
Design Pattern
ServiceProvider.java

public class ServiceProvider {
/* Implement common method
public Inquiries getInquiry (String inquiryType)
{
public class ServiceProvider {
/* Implement common methodpublic Inquiries
getInquiry (String inquiryType)
{
if (inquiryType.equals(“Saving”)) {
SavingInquiryBank bank = new
SavingInquiryBank ();SavingInquiries
savinginquiries =
(SavingInquiries)bank.getInquiry ();
return savinginquiries;
}
else if (inquiryType.equals(“Current”)) {
CurrentInquiryBank bank = new
CurrentInquiryBank ();
CurrentInquiries currentinquiries =
(CurrentInquiries)bank.getInquiry ();
return currentinquiries;
}
else {
CorporateInquiryBank bank = new
CorporateInquiryBank ();
CorporatetInquiries corporatetinquiries =
(CorporateInquiries)
bank.getInquiry ();
return corporatetinquiries;
}}// End of class

This is clear that the complex implementation will be done by ServiceProvider himself. The client will just access the ServiceProvider and ask details of either saving, current orcorporate inquiries. The client program can now create an object of ServiceProvider class and call method getInquiry() passing as parameter the type of inquiry required. This can be done as follows. new ServiceProvider().getInquiry(“Saving”); Here is a simple code of the client program that accesses this façade for getting saving account information.

   Client.java

public class Client {
/*** to get saving inquiries*/
public static void main(String[] args) {
ServiceProvider provider = new
ServiceProvider().getInquiry(“Saving”);
} }
// End of class

III. Flyweight Design Pattern

The Flyweight is a software design pattern, useful when there is the need for multiple objects sharing some common information. In programming languages, some times you may need to generate a very large number of small class instances to represent the entire system. This is usually very memory consuming to keep track of those objects. The Flyweight Pattern reduces this problem and avoids the overhead of large numbers of very similar class-objects. The Flyweight design pattern provides an approach for handling the classes in a system where these classes are maintained through the extrinsic data that is passed in as arguments. 

When to use Flyweight Pattern:
• The application requires a large number of objects.
• Storage costs are high and difficult to maintain the number of objects.
• The application doesn’t depend on object identity.

Benefits:

• Reduction of number of Objects to handle.
• Reduction in memory and on storage devices, if objects are persisted.

Oct 2007 | Java Jazz Up | 40
 
previous
index
next
 
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