Facade Pattern - Canisius College Computer Science

advertisement
Computer Science 313 – Advanced Programming Topics
LECTURE 27:
FAÇADE PATTERN
“In a Nutshell”
 Commonly used English expression
 Means “in a few words” or “briefly”
 Summarizing idea with many details left out
I could
be
bounded
in

a nutshell, and count

my selfe a King of

infinite space.
“In a Nutshell”
 Usage can be traced back to 1608
 Even before Hamlet used it
 Meaning has not changed in 400 years
 Also became a verb in that time
 “To nutshell” popularized by Mark Twain
 Any questions about what it means?
“In a Nutshell”
 Usage can be traced back to 1608
 Even before Hamlet used it
 Meaning has not changed in 400 years
 Also became a verb in that time
 “To nutshell” popularized by Mark Twain
 Any questions about what it means?
 WILL SOMEONE EXPLAIN THIS TO O’REILLY!
Big Picture for Façade
 Provide simple interface to low-level functions
 Create single class defining simple methods
 May combine functionality of multiple instances
 Façade makes client easier to write & use
 Packages tricky parts into simple method(s)
 Allows client to avoid working with many instances
 Nothing should change in lower level classes
 Using the façade complex subsystem hidden
 But still usable without going through façade
Real-Life is Hard
 Real life is full of complex ideas & concepts
 Filling out your tax forms every April
 Rules for holding in (American) football
 Explaining why the sky is blue
 Appeal of soccer to billions worldwide
Real-Life is Hard
 Often rely on simplicities to understand these
 Filling out your tax forms every April
“Politician got
contribution to create deduction”
 Rules for holding in (American) football
“Stay within the shoulder pads & don’t get caught”
 Explaining why the sky is blue
“Wavelengths not scattered by the atmosphere”
 Appeal of soccer to billions worldwide
“Excuse to drink beer & occasionally riot”
Real-Life Façade
 Compiler is (real-life) example of façade
 Typically we think of compiler as single system
 Many complex systems combine to make compiler
 Code divided into tokens by the parser
 Lexigraphical analyzer computes tokens’ meanings
 Analysis improved & simplified by optimizer
 Code generator lowers & outputs result
 Even so, many of these systems also façades:
 Loop analyzer, SSA transform, peepholer in optimizer
Façades On Computers
Façade We Regularly Use
 Usually do all interactions via simple interface
 javac foo.java
compiles class named foo
 But can also access components directly
 final & volatile provide details to optimizer
 Can get instance of javax.tools.JavaCompiler
Façade We Regularly Use
 Compilation easy because of this façade
 Façade simplifies & allows messing with internals
 All skill levels helped using the compiler
 Beginner
javac foo.java
 Expert
javac –g:none foo.java
 Stupid
javac –nowarn –Xprint foo.java
Façade versus Adapter
 Façade and Adapter patterns very similar
 Enable simplifying code to be used by client
 Both patterns effectively hide subsystem details
 1 or more instances behind exposed interface
 Idealized UML class diagram may/may not differ
 “What is the difference?” should be asked
Intent Crucial to Usage
 Adapter makes existing code use interface
 Original implementation hidden by pattern
 Lets client code think class written for interface
 Adapts existing code so it can work in other places
 Façade used to create interface to use system
 Original implementation hidden by pattern
 Lets client write code as if design has simple interface
 Provides a false front for use in another system
Clients of Façade & Adapter
 With either pattern within client code:
No difference in usage
No difference in design
Using Façade & Adapter
Façade
Adapter
\
Yams
Sweet Potatoes
Using Façade & Adapter
Façade
Adapter
\
Sweet Potatoes
Yams
Kissing Cousins of Patterns
 Adapter & Façade also close to Decorator
 Structural patterns that help simplify client code
 Within each pattern, hide implementation details
 Workings invisible to those outside pattern
 Decorator has some important differences
 Decorations invisible; adapter & façade not
 Decorator requires specific class relationships
 Interface unchanged by decorator pattern
For Next Class
 Lab #6 due in one week
 Do not leave labs to last second
 Read pages 265 – 275 for Friday
 What is the Principle of Least Knowledge?
 How much of class already follows this principle?
 What is the Law of Demeter?
 Can we quantify this principle?
Download