cs 325: software engineering march 17, 2015 applying patterns (part a) the façade pattern the...
TRANSCRIPT
CS 325: Software Engineering
March 17, 2015
Applying Patterns (Part A)• The Façade Pattern• The Adapter Pattern• Interfaces & Implementations• The Strategy Pattern• The Bridge Pattern• The Abstract Factory Pattern• Designing Like An Expert
CS 325March 17, 2015Page 2
The Façade Pattern
For instance, when certain elements of the client community need access to the functionality provided by a complex subsystem, it is undesirable to expose the client to the intricacies of interacting with this complicated subsystem.
The Façade Pattern provides a unified interface to the subsystem, simplifying the client’s ability to use the subsystem.By encapsulating the complex subsystem within a single interface object, this pattern reduces the learning curve associated with using the subsystem and decouples the subsystem from its clients.
Some design patterns are structural, describing how to combine objects into larger structures.
CS 325March 17, 2015Page 3
The Façade Pattern: An Example
The Façade Pattern provides a simple interface to an entire subsystem of objects, simplifying the use of the subsystem by reducing the need for outside code to be aware of the subsystem’s inner workings, thereby improving the readability of the clients that use the subsystem.
Applying the Façade Pattern to a compiler.
CS 325March 17, 2015Page 4
The Adapter PatternAnother structural pattern adapts the public members (or interface) of one class into those of another.When the client expects to interact with a class in a particular manner, it is sometimes necessary to “adapt” the interface of the class to accommodate those expectations.The client will call the
adapter’s interface which, in turn, translates those calls into calls to the original interface of the class being adapted.
This permits classes to work together in spite of incompatible interfaces.
CS 325March 17, 2015Page 5
The Adapter Pattern: An ExampleOne common use of the Adapter Pattern is to adapt old legacy code to a new interface.
The client uses the (x,y) position of a Rectangle’s upper left corner and the Rectangle’s width and height to draw the Rectangle.The old LegacyRectangle uses the (x,y) positions of its upper left and lower right corners to draw it.
The RectangleAdapter adapts the old-style drawing to the new style by converting the client’s information about the one corner, the width, and the height into the LegacyRectangle’s information about the two corners.
CS 325March 17, 2015Page 6
Interfaces & ImplementationsTo fully exploit the benefits of object-oriented design and programming, the implementation of a software module should be distinguished from its interface.
Implementation
• Actual code of procedures and methods
• All private data members and member functions
• Allows for internal modification without affecting the way outside entities interact with the module
Interface
• Provides abstraction of module to outside entities
• Restricts access to resources to well-defined entry points, such as public members
• Supports polymorphism, often via inheritance from the same abstract class
CS 325March 17, 2015Page 7
The Strategy PatternAnother type of design pattern is the behavioral pattern, which addresses common object interactions.In the Strategy Pattern, each algorithm in a family of algorithms is encapsulated and they are all made interchangeable, thus allowing the algorithm to vary independently of the clients that use it.This permits a program to
switch easily between algorithms without resorting to monolithic if-else statements.This approach allows the behavior in question to be modified at run-time and not just at design time.
CS 325March 17, 2015Page 8
The Strategy Pattern: An ExampleThe strategy could be the particular sorting algorithm used to organize a list.
Instead of selecting the specific algorithm within the code, a run-time selection can be made, based upon the particular data set being sorted.In this way, the particular sorting
algorithm chosen can accommodate the data set in question, with the sorting algorithm selected on the basis of which one provides optimal performance for the immediate data set.
CS 325March 17, 2015Page 9
The Bridge PatternThere are times when the interface and the implementation of a class need to vary independently.When the abstract interface and the concrete implementation have been set up as parallel class hierarchies, it becomes difficult to independently extend either the interface or the implementation.The Bridge Pattern addresses
this problem by decoupling the two class hierarchies, making them orthogonal rather than parallel.In this way, the abstract interfaces can be platform-independent even when the concrete implementations are platform-dependent.
CS 325March 17, 2015Page 10
The Bridge Pattern: An ExampleConsider the notion of drawing shapes using different graphics platforms.
By decoupling the Shape abstraction from the Drawing implementor, the explosion of platform-dependent shape subclasses is avoided and the amount of redundant code is minimized.
With this model, individual shape alterations and additional extensions to the Shape class will only affect the abstraction portion of the model, while platform modifications will only affect the implementor portion of the model.
CS 325March 17, 2015Page 11
The Abstract Factory PatternA third type of design pattern is the creational pattern, which addresses class instantiation problems.If an application needs to be portable, it needs to encapsulate platform dependencies.These platforms might include the operating system, the database management system, the windowing system, the web browser, etc. Frequently, this encapsulation isn’t
engineered in advance, resulting in a massive proliferation of #ifdef case statements with options for all currently supported platforms.Rather than having clients create platform objects directly, they create abstract objects and utilize an abstract factory as an interface for producing the concrete objects.
CS 325March 17, 2015Page 12
The Abstract Factory Pattern: An ExampleConsider the development of shape-generation code that would work on two different graphics platforms.The client uses the
abstract ShapeFactory to produce abstract curved and straight shapes.If the platform has been established as “simple”, concrete Circle and Square objects are created, while if the platform has been established as “robust”, concrete Ellipse and Rectangle objects are created.
CS 325March 19, 2015Page 13
Designing Like An ExpertChristopher Alexander advocated a pattern-based approach to design that may be applied to software.
Essentially, the idea is to always keep context in mind.Don’t concern yourself with instantiation problems until you’ve addressed structural issues.And don’t concern yourself with structural issues until you’ve considered how the class behaves.
Begin with a conceptual understandi
ng of the design
requirements.
Identify the patterns
that characterize
the entire design.
Work inwards from the
behavior of a class to its
structure and
ultimately to its creation.
Apply the discovered patterns in
order, refining the
overall design in
the process.
CS 325March 17, 2015Page 14
Designing Like An Expert: Determining Senior Patterns
Adapting an interface to
another interface requires that
those interfaces be established
first. The Bridge pattern sets up the structure of the interfaces, a prerequisite to the Adapter’s
mapping of those interfaces.
The Abstract Factory pattern
depends on knowing which classes will be defined. The
Façade pattern sets up a
structure for accessing those classes. In that
sense, the Façade should be
established before
considering the details required by the Abstract
Factory.
The Adapter pattern is
concerned with the structure of
both the Adapter and the Adaptee.
The Strategy pattern considers how classes will interact. Those
behavioral issues need to be
addressed before considering the
issues associated with basic structure.
Adapter or
Bridge?
Abstract
Factory or
Façade?
Strategy or Adapt
er?
During the course of your design, you observe potential problems that might be addressed via two separate design patterns. Which pattern should you apply first?