280 likes | 435 Views
Lect 35 - OO Design Patterns. Patterns are commonly used object-oriented design ideas or paradigms, separate from any particular language or implementation. “The Patterns Book” - a catalogue of common patterns:
E N D
Lect 35 - OO Design Patterns Patterns are commonly used object-oriented design ideas or paradigms, separate from any particular language or implementation. “The Patterns Book” - a catalogue of common patterns: Design Patterns - Elements of Reusable Object-Oriented Software, by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Pub. Addison-Wesley, 1994, ISBN- 2-201-63361-2. OO Programming
Abstract Factory(87) Builder (97) Factory Method (107) Prototype (117) Singleton (127) Page numbers of the book given in brackets. Abstract the instantiation process and help make a system independent of how its objects are created. Useful when a system evolves to depend more on object composition than class inheritance. Creational Patterns OO Programming
Adapter (139) Bridge (151) Composite (163) Decorator (175) Facade (185) Flyweight (195) Proxy (207) Concerned with how classes and objects are composed to form larger structures. Structural Patterns OO Programming
Chain of Responsibility (223) Command (233) Interpreter (243) Iterator (257) Mediator (273) Memento (283) Observer (293) State (305) Strategy (315) Template Method (325) Visitor (331) Concerned with algorithms and the assignment of responsibilities between objects. Describe patterns of communication between objects and characterize what may be difficult to follow runtime control flow. Behavioral Patterns OO Programming
Provide an interface for creating families of related or dependent objects without specifying their concrete classes. Also known as “Kit” eg WidgetFactory in windowing system such as Motif or… Isolates concrete classes Makes exchanging product families easy Promotes consistency amongst products Abstract Factory (87) OO Programming
Separate the construction of a complex object from its representation so that the same construction process can create different representations. Client can use to build objects independent of the parts that are needed and how they are assembled eg text conversion Builder (97) OO Programming
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses. aka “Virtual Constructor” Useful when a class cannot anticipate the class of objects it must create Delegate to its subclasses Connects separate class hierarchies Factory Method (107) OO Programming
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. Generate s new objects based on clones of prototype. Allows addition & removal of products at run time Prototype (117) OO Programming
Ensure a class only has one instance, and provide a global point of access to it. Supports controlled access to sole instance Less namespace pollution than global variables One single port of call Singleton (127) OO Programming
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that could not otherwise because of incompatible interfaces. aka “Wrapper” Wraps around an object to modify its interface to be compatible with client’s expectations Adapter (139) OO Programming
Decouple an abstraction from its implementation so that the two can vary independently. aka “Handle/Body” Helps avoid permanent binding between an abstraction and its implementation Helps hide implementation details from clients Bridge (151) OO Programming
Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. Useful to represent part-whole hierarchies of objects Allows clients to ignore differences between compositions of objects and individual objects Composite (163) OO Programming
Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for extending functionality. aka “Wrapper” Allows responsibilities to be added dynamically and transparently Useful for when added responsibilities may be later withdrawn Decorator (175) OO Programming
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. Makes life easier for the majority of clients while not concealing functionality from a minority that need it. Reduces client-subsystem coupling Facade (185) OO Programming
Use sharing to support large numbers of fine-grained objects efficiently. A shared object that can be used in multiple contexts simultaneously Client responsible for extrinsic state of the flyweights Useful when storage costs prohibit large numbers of heavy-weight objects Flyweight (195) OO Programming
Provide a surrogate or placeholder for another object to control access to it. aka “Surrogate” Acts as stand-in, can be: Remote proxy Virtual proxy Protection proxy Smart reference Proxy (207) OO Programming
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. Useful for handlers eg exception handling event handling Allows reduced coupling Receipt not guaranteed Added flexibility in assigning responsibilities to objects Chain of Responsibility (223) OO Programming
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. Aka “Action” & “Transaction” Useful when do not know the exact receiver of one of our issued commands Commands become first class objects Command (233) OO Programming
Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language. General so easy to change and extend the grammar Useful when grammar is simple And when efficiency not too critical eg regular expressions for pattern matching in strings Interpreter (243) OO Programming
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. aka “Cursor” Generalises traversal over container objects contents Still hides internal implementation Robustness may be an issue if removals may happen mid-iteration Iterator (257) OO Programming
Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. Centralises control Decouples colleagues Limits sub-classing Simplifies object protocols Encapsulates how cooperation is done Mediator (273) OO Programming
Without violating encapsulation, capture and externalize an objects internal state so that the object can be restored to this state later. aka “Token” Useful for saving state Helps implement persistence May be expensive eg object serialization Memento (283) OO Programming
Define a non-to-many dependency between objects to that when one object changes state, all its dependents are notified and updated automatically. aka “Dependents” & “Publish-Subscribe” & “Listener” Support for broadcast communication Unexpected updates Observer (293) OO Programming
Allow an object to alter its behavior when its internal state changes. The object will appear to change its class. aka “Objects for States” Useful when object’s behavior depends on its state and must change at runtime Also when operations have large multipart conditional statements that depend on the state. State (305) OO Programming
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. aka “Policy” Useful when many different classes differ only in their behavior and when need different variants of an algorithm and when an algorithm uses data that clients should not know about Strategy (315) OO Programming
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure. Used to implement the invariant parts of an algorithm letting subclasses customise the variant parts Re-factoring to generalise Template Method (325) OO Programming
Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates. Useful when object structure contains many classes of objects with differing interfaces and want to perform operations on these that depend on their concrete classes Allows grouping of related operations Visitor (331) OO Programming
See the Patterns Book. defines the catalogue names of patterns Web sites - patterns is an ongoing development Annual Conferences like OOPSLA Not a new idea - see algorithms books too - eg The Art of Computer Programming, by D.Knuth, Addison-Wesley 1973. Note the key idea of separating ideas & concepts from the implementation & specific language used. Patterns Summary OO Programming