Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Facade Pattern (Structural) ©SoftMoore ConsultingSlide 1.

Similar presentations


Presentation on theme: "The Facade Pattern (Structural) ©SoftMoore ConsultingSlide 1."— Presentation transcript:

1 The Facade Pattern (Structural) ©SoftMoore ConsultingSlide 1

2 Motivation A subsystem is a group of related functions, classes, and interfaces. Structuring a system into subsystems helps reduce complexity, but the interface exposed by the components of a subsystem can be quite complex. A developer who wants to use the subsystem may not know where to begin. A common design goal is to minimize the communication and dependencies between subsystems. One way to achieve this goal is to introduce a façade object that provides a single, simplified interface to the more general facilities of a subsystem. ©SoftMoore ConsultingSlide 2

3 Motivation (continued) Façade Pattern: Basic Idea Create a Façade class to abstract/encapculate the interface to a subsystem. Clients communicate with subsystem classes through the Façade. The Façade forwards messages to the correct subsystem classes. The Façade may adapt the interface of the subsystem. Clients can always use subsystem classes directly if necessary. ©SoftMoore ConsultingSlide 3

4 Motivation (continued) ©SoftMoore ConsultingSlide 4 Subsystem without a Façade subsystem classes Client

5 Motivation (continued) ©SoftMoore ConsultingSlide 5 Subsystem with a Façade subsystem classes Façade Client

6 Façade Pattern Intent: Provide a unified interface to a set of classes and interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use. Applicability: Use the Façade pattern when –you want to provide a simple interface to a complex subsystem. A façade can provide a simple default view of the subsystem that is good enough for most clients. –there are many dependencies between clients and implementation classes of an abstraction. Introduce a façade to decouple the subsystem from clients and other subsystems, thereby promoting subsystem independence and protability. –you want to layer your subsystems (architectural pattern). Use a façade to define an entry point to each subsystem. Slide 6©SoftMoore Consulting

7 Façade Pattern (continued) ©SoftMoore ConsultingSlide 7 Structure subsystem classes Façade

8 Façade Pattern (continued) Participants Façade –knows which subsystem classes are responsible for a request –delegates client requests to appropriate subsystem classes Subsystem classes –implement subsystem functionality –handle work assigned by the Façade object –have no knowledge of the façade; i.e., they keep no references to it. Slide 8©SoftMoore Consulting

9 Façade Pattern (continued) Collaborations Clients communicate with the subsystem by sending requests to Façade, which forwards them to the appropriate subsystem object(s). Although the subsystem objects perform the actual work, the façade may have to do work of its own to translate its interface to subsystem interfaces. Clients that use the façade don’t usually access the subsystem objects directly. ©SoftMoore ConsultingSlide 9

10 Façade Pattern (continued) Consequences: The Façade pattern shields clients from subsystem components, thereby reducing the number of objects that clients deal with and making the subsystem easier to use. promotes weak coupling between the subsystem and its clients. –lets you vary the components of the subsystem without affecting its clients –reduces compilation dependencies doesn’t prevent applications from using the subsystem classes if they need to. ©SoftMoore ConsultingSlide 10

11 Façade Pattern (continued) Implementation Coupling between clients and the subsystem can be reduced even further by making Façade an abstract class with concrete subclasses for different implementations of a subsystem. It can be useful to think of the public and private parts of a subsystem, where the public interface consists of classes that can be accessed by clients. Façade encapsulates “most” of a subsystem’s public interface. Subsystem classes can be grouped together in a Java package, a C++ namespace, or similar grouping mechanisms in other languages. ©SoftMoore ConsultingSlide 11

12 Façade Pattern Example 1: Compiler A compiler consists of many classes that perform functions such as scanning, parsing, structuring into abstract syntax trees, semantic analysis, optimization, and code generation. Most clients of a compiler don’t care about the details of these classes – they merely want to compile some code. To provide a higher-level interface for its clients, a compiler subsystem can also provide a Compiler class as a façade. ©SoftMoore ConsultingSlide 12

13 Façade Pattern Example 1: Compiler (continued) public class Compiler {... public void compile(FileReader sourceFileReader, OutputStream targetStream) throws IOException { Source source = new Source(sourceFileReader); Scanner scanner = new Scanner(source); Parser parser = new Parser(scanner); ErrorHandler errorHandler = ErrorHandler.getInstance(); printProgressMsg("Starting compilation..."); ©SoftMoore ConsultingSlide 13 Note Singleton (continued on next slide)

14 Façade Pattern Example 1: Compiler (continued) // parse source file Program prog = parser.parseProgram(); // check constraints if (!errorHandler.errorsExist()) { printProgressMsg("Checking constraints..."); prog.checkConstraints(); } // optimize if (!errorHandler.errorsExist()) { printProgressMsg("Optimizing..."); prog.optimize(); } ©SoftMoore ConsultingSlide 14 (continued on next slide)

15 Façade Pattern Example 1: Compiler (continued) // generate code if (!errorHandler.errorsExist()) { printProgressMsg("Generating code..."); prog.setOutputStream(targetStream); prog.emit(); } if (errorHandler.errorsExist()) printProgressMsg("Errors detected " + "-- compilation terminated."); else printProgressMsg("Compilation complete."); } ©SoftMoore ConsultingSlide 15

16 Façade Pattern Example 2: Email JavaMail provides a complete API for creating and sending messages including classes that support –message headers– message body –attachments– security A façade can simplify the creation of simple messages and shield most clients from the complexity of the JavaMail API. ©SoftMoore ConsultingSlide 16 Note: A builder pattern could also be used to simplify the creation of simple messages.

17 Façade Pattern Example 2: Email (continued) public class EmailUtil {... public static void send(String host, String username, String password, String from, String[] to, String subject, String msgText) throws MessagingException {... } public static boolean isValidEmailAddr(String emailAddr) {... } ©SoftMoore ConsultingSlide 17

18 Façade Pattern in Java (David Geary) The Java Swing GUI classes provide extensive facilities for windows and dialog boxes, but it is rather tedious and complicated to create a simple dialog box from scratch. Swing provides a façade class JOptionPane that simplifies the creation and use of the most common types of dialog boxes. ©SoftMoore ConsultingSlide 18

19 Façade Pattern in Java (continued) ©SoftMoore ConsultingSlide 19 JPanel BorderFactory Dialog JDialog BorderLayout JOptionPane Client Window FlowLayout

20 Java Enterprise Pattern: Session Façade A number of books and articles on Java Enterprise recommend using a session façade to encapsulate the complexity of interactions between the business objects participating in a workflow. The Session Facade manages the business objects, and provides a uniform coarse-grained service access layer to clients. Clients access a Session Façade instead of accessing business components directly. ©SoftMoore ConsultingSlide 20

21 Java Enterprise Pattern: Session Façade (continued) ©SoftMoore ConsultingSlide 21 Client «EJBSession» SessionFacade BusinessObject «EntityEJB» BusinessEntity «SessionEJB» BusinessSession DataAccessObject 1..*

22 Related Patterns Abstract Factory can be used with Façade to provide an interface for creating subsystem objects in a subsystem- independent way. Mediator is similar to Façade in that it abstracts functionality of existing classes. However, a mediator’s colleagues are aware of and communicate with the mediator instead of communicating with each other directly. In contrast, a façade’s subsystem classes are unaware of the façade. Usually only one Façade object is required. Thus Façade objects are often Singleton. ©SoftMoore ConsultingSlide 22

23 References Façade pattern (Wikipedia) http://en.wikipedia.org/wiki/Facade_pattern Façade Design Pattern (SourceMaking) http://sourcemaking.com/design_patterns/facade Façade clears complexity by David Geary (JavaWorld) http://www.javaworld.com/article/2073463/swing-gui-programming/fa--231-ade- clears-complexity.html Session Façade (Core J2EE Patterns) http://www.corej2eepatterns.com/Patterns2ndEd/SessionFacade.htm http://www.oracle.com/technetwork/java/sessionfacade-141285.html ©SoftMoore ConsultingSlide 23


Download ppt "The Facade Pattern (Structural) ©SoftMoore ConsultingSlide 1."

Similar presentations


Ads by Google