Facade pattern
From Wikipedia, the free encyclopedia
The facade pattern or façade pattern is a software engineering design pattern commonly used with Object-oriented programming.
A facade is an object that provides a simplified interface to a larger body of code, such as a class library. A facade can:
- make a software library easier to use and understand, since the facade has convenient methods for common tasks;
- make code that uses the library more readable, for the same reason;
- reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system;
- wrap a poorly designed collection of APIs with a single well-designed API (As per task needs).
An Adapter is used when our wrapper must respect a particular interface and must support a polymorphic behavior. On the other hand, a facade is used when one wants an easier/simpler interface to work with.
Contents |
[edit] Structure
- Facade
- The facade class interacts Packages 1, 2, and 3 with the rest of the application.
- Clients
- The objects using the Facade Pattern to access resources from the Packages.
- Packages
- Software library / API collection accessed through the Facade Class.
[edit] Examples
[edit] Java
The following example hides the parts of a complicated calendar API behind a more user friendly facade. The output is:
Date: 1980-08-20 20 days after: 1980-09-09
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** "Facade" * */ class UserfriendlyDate { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); public UserfriendlyDate(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()); } } /** "Client" * */ class FacadePattern { public static void main(String[] args) throws ParseException { UserfriendlyDate d = new UserfriendlyDate("1980-08-20"); System.out.println("Date: " + d.toString()); d.addDays(20); System.out.println("20 days after: " + d.toString()); } }
[edit] C#
// Facade pattern -- Structural example using System; namespace DoFactory.GangOfFour.Facade.Structural { // Mainapp test application class MainApp { public static void Main() { Facade facade = new Facade(); facade.MethodA(); facade.MethodB(); // Wait for user Console.Read(); } } // "Subsystem ClassA" class SubSystemOne { public void MethodOne() { Console.WriteLine(" SubSystemOne Method"); } } // Subsystem ClassB" class SubSystemTwo { public void MethodTwo() { Console.WriteLine(" SubSystemTwo Method"); } } // Subsystem ClassC" class SubSystemThree { public void MethodThree() { Console.WriteLine(" SubSystemThree Method"); } } // Subsystem ClassD" class SubSystemFour { public void MethodFour() { Console.WriteLine(" SubSystemFour Method"); } } // "Facade" class Facade { SubSystemOne one; SubSystemTwo two; SubSystemThree three; SubSystemFour four; public Facade() { one = new SubSystemOne(); two = new SubSystemTwo(); three = new SubSystemThree(); four = new SubSystemFour(); } public void MethodA() { Console.WriteLine("\nMethodA() ---- "); one.MethodOne(); two.MethodTwo(); four.MethodFour(); } public void MethodB() { Console.WriteLine("\nMethodB() ---- "); two.MethodTwo(); three.MethodThree(); } } }
[edit] External links
- Description from the Portland Pattern Repository
- Description from the Net Objectives Repository
- Jt J2EE Pattern Oriented Framework
[edit] Footnotes
|