Download presentation
Presentation is loading. Please wait.
Published byClyde Chapman Modified over 9 years ago
2
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
3
CS 325 March 17, 2015 Page 159 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.
4
CS 325 March 17, 2015 Page 160 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.
5
CS 325 March 17, 2015 Page 161 The Adapter Pattern Another 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.
6
CS 325 March 17, 2015 Page 162 The Adapter Pattern: An Example One 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.
7
CS 325 March 17, 2015 Page 163 Interfaces & Implementations To fully exploit the benefits of object-oriented design and programming, the implementation of a software module should be distinguished from its interface. 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 Implementation 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 Interface
8
CS 325 March 17, 2015 Page 164 The Strategy Pattern Another 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.
9
CS 325 March 17, 2015 Page 165 The Strategy Pattern: An Example The 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.
10
CS 325 March 17, 2015 Page 166 The Bridge Pattern There 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.
11
CS 325 March 17, 2015 Page 167 The Bridge Pattern: An Example Consider 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.
12
CS 325 March 17, 2015 Page 168 The Abstract Factory Pattern A 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.
13
CS 325 March 17, 2015 Page 169 The Abstract Factory Pattern: An Example Consider 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.
14
CS 325 March 19, 2015 Page 170 Designing Like An Expert Christopher 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 understanding 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.
15
CS 325 March 17, 2015 Page 171 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 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. 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 Adapter? 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?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.