Date post: | 30-Dec-2015 |
Category: |
Documents |
Upload: | rose-washington |
View: | 239 times |
Download: | 2 times |
Structural Patterns
• deal with the composition of classes or objects.
• Structural class patterns:– use inheritance to compose classes
• Structural object patterns:– describe ways to assemble objects.
Façade Pattern
• Problem– There may be undesirable coupling to many things
in the subsystem, or the implementation of the subsystem may change.
– What to do?
Façade - Example• Starting the HomeTheater• Put the screen down• Turn on the projector• Dim the lights• Set the projector input to
DVD• Turn the amplifier on• Set the amplifier to DVD
input• Turn the DVD player on• Start the DVD Player playing
• Turning everything off at the end• Complex task to listen music• When upgraded or new things added, you would have to
learn new steps
Lights, Camera, Façade!
• A Façade is what you need.• With the façade pattern, you can take a
complex subsystem and make it easier to use by implementing a façade class that provides a reasonable set of interfaces.
Façade - Definition
• Provides a unified interface to a set of interfaces in a sub-system.
• Defines a higher level interface that makes the subsystem easier to use.
Facade
• Problem– There may be undesirable coupling to many things in the
subsystem, or the implementation of the subsystem may change. What to do?
• Solution– Define a single point of contact to the subsystem facade
object that wraps the subsystem. – This facade object presents a single unified interface and is
responsible for collaborating with the subsystem components
Façade - Consequences
• It shields clients from subsystem components, thereby reducing the number of objects that clients deal with and making the subsystem easier to use
• It promotes weak coupling between the subsystem and its clients. Weak coupling lets you vary the components of the subsystem without affecting its clients
• It doesn't prevent applications from using subsystem classes if they need to.
Façade vs. Adapter• With both the Facade and Adapter pattern, we have
preexisting classes.• With the Facade, however, we do not have an interface we
must design to, as we do in the Adapter pattern.• We are not interested in polymorphic behavior with the
Facade; whereas with the Adapter, we probably are. • In the case of the Facade pattern, the motivation is to simplify
the interface. With the Adapter, although simpler is better, we try to design to an existing interface and cannot simplify things even if a simpler interface were otherwise possible.