The Facade Design Pattern (II)
9 March 2008This post is in continuation of The Facade Design Pattern (I). Do read that before this one.
Structure of the Facade Design Pattern
Facade: It hides the parts of a complicated API behind a more easy to understand and user friendly facade. The facade class interacts with different Packages with the rest of the application.
Clients: The objects using the Facade Pattern to access resources from the Packages.
Packages: Software library or API collection are accessed through the Facade Class.
The example below demonstrates use of facade design pattern. If you want to add a few days to a date, you would require to use a complicated calendar API with not that easy to understand code. With the help of facade design pattern you can write easy to understand and easy to use methods to add days you want to a date.
import java.text.*; import java.util.*; // This is is facade class MyFacadeDate { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); public MyFacadeDate (String isodate_ymd) throws ParseException { Date date = sdf.parse(isodate_ymd); cal.setTime(date); } public void addDays (int days) { cal.add (Calendar. DAY_OF_MONTH, days); } public String toString() { return sdf.format(cal.getTime()); } } // This is a client class FacadeDemo { public static void main(String[] args) throws ParseException { MyFacadeDate d = new MyFacadeDate("2005-09-03"); System.out.println ("Date: " + d.toString()); d.addDays(20); System.out.println ("20 days after: " + d.toString()); } }
Output:
Date: 2005-09-03
20 days after: 2005-09-23
Related Posts:
- The Facade Design Pattern (I)
- Singleton - Creational Design Pattern (I)
- The Strategy Design Pattern in Java(I)
- Adapter design pattern (II)
- The Observer Design Pattern(I)
- Adapter design pattern
- Design Patterns - Basics (I)
- The Strategy Design Pattern in Java(II)
- Singleton - Creational Design Pattern (II)
- Behavioral Pattern - Iterator Pattern (Example) - 1
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: The Facade Design Pattern (II)