110 likes | 222 Views
Chapter 10. Design Patterns. Motivation for Design Patterns. Most software systems contain certain common aspects that are frequently reinvented for each system Solutions to these common problems may vary in quality from system to system
E N D
Chapter 10 Design Patterns
Motivation for Design Patterns • Most software systems contain certain common aspects that are frequently reinvented for each system • Solutions to these common problems may vary in quality from system to system • Design patterns seeks to communicate these classic solutions in an easy to understand manner
What are Design Patterns? • Design Patterns communicate solutions to common programming problems • The seminal book on design patterns, Design Patterns, Elements of Reusable Object-Oriented Software by Gamma et al, identifies three categories of design patterns • Creational • Structural • Behavioral
Creational Patterns • Abstract factory • Facilitates the creation of related objects without concrete class reference • Builder • If a class supports a number of different data representations, the builder encapsulates the details of the necessary conversions • Factory method • Defers determination of specific class to create to a subclass
More Creational Patterns • Prototype • Embodies information comprising prototypical instances • Singleton • Ensures that the class has at most one instance and that other classes have global access to it
Structural Patterns • Adapter • Translates between interacting objects for which the method signatures are incompatible • Bridge • Creates a more flexible mechanism for multiple implementations of an abstract class than inheritance • Composite • Facilitates a generic hierarchical grouping of objects where the class and number of objects may change • Decorator • Allows dynamic addition of new behavior to objects by acting as an outer wrapper containing the added features
More Structural Patterns • Facade • Provides a front end for a series of classes so that users of these classes only need to know of and use the standard set of methods implemented by the facade • Flyweight • Describes a strategy for using an otherwise prohibitive number of very small-scale objects with minimal overhead • Proxy • A means by which the instantiation of certain classes may be delayed until the proxied object is needed
Behavioral Patterns • Chain of responsibility • A strategy that sets up a series of objects that may respond to a message depending on the context of the request • Command • Allows methods to be encapsulated in objects and invoked by a standard means as determined by the interface of the common class • Interpreter • If a set of problems are conducive to being represented as a simple grammar, a class may be defined for each rule
More Behavioral Patterns • Iterator • Separates the class elements necessary for access and traversal from the aggregating class and encapsulates them in the iterator • Mediator • Helps to avoid highly coupled class definitions by moving explicit object references to an intermediary object • Memento • A means to store the otherwise encapsulated internal state of an object for the purpose of creating a check-point or undoing transactions
More Behavioral Patterns • Observer • Allows multiple dependent instances of a class to be notified of relevant changes to the observed class • State • A class hierarchy is defined around a set of states for an object, allowing the behavior of an object to vary depending on a state by substituting the state-specific subclass in its composition • Strategy • Selects a set of related algorithms and encapsulates them in such a way that they may be interchangeable for solving certain problems
More Behavioral Patterns • Template method • Structures a method in such a way that elements of the algorithm are deferred to subclasses • Visitor • Allows the introduction of new operations on the objects that comprise the visited object