Download presentation
Presentation is loading. Please wait.
Published byLesley Watts Modified over 9 years ago
1
Interface Patterns
2
Adapter Provides the interface a client expects, using the services of a class with a different interface Note Avoid using object adapter if possible because the adaptee could change and make private things the adapter uses. Errors like this are less likely to be picked up at compile time. Example JTable – You adapt an array of your custom objects to provide the TableModel interface that JTable needs Facade Provides a simplified interface making a subsystem easy to use. It lies somewhere between a toolkit and a complete application. Example JOptionPane provides a simplified way to input choices based on JPanel and JButtons Composite Lets clients treat individual objects and compositions of objects uniformly. Note composites may include more than one leaf class or composites with varied implementations of the operation method. However all must implement the component interface. Example Tree and directory structures. Bridge Decouples an abstraction from its implementations so that they can vary independently. Example Database drivers / Printer drivers
3
Responsibility Patterns
4
Singleton Ensures a class has only a single instance… Resists the distribution of responsibility. Note Use a private constructor and create a method that calls it only once. Ensure the method is declared final. Example J2ME runtime class. Drivers that are responsible for controlling hardware. Observer Example JSlider.addChangeListener() Event passing in the model - view – controller (MVC) pattern Mediator Takes responsibility off several objects. Defines an object that encapsulates how a set of objects interacts. Note The attached objects being mediated are usually of different classes and often send events to the mediator. Defining the interfaces the pattern uses (lower diagram) seems wise. Example Accessing a database to reflect real world changes. Creating a GUI with lots of inter-related GUI components Proxy Provides a surrogate or placeholder for another object. Examples Image proxies – for while the true image is being downloaded Network resource proxys RMI / CORBA – for remote method invocation Dynamic proxies using JAVA reflection Defines a one to many dependency so when an object changes all its dependents are notified and update themselves automatically. Note In some cases a simplified subject allows only a single observer to be attached. Attempting to attach another will overwrite the first one or cause an exception to be thrown.
5
Chain of responsibility Avoids coupling the sender of a request to receiver. The request is passed along a chain until it reaches an object able to handle it. Example Parser chains to decode an input that could be in several formats. Thrown exceptions that travel up the call stack until they find a valid catch block. Customer support telephone networks. Traversing a composite (such as up a scene graph to find your position). Event handling in windows Visual C++. Flyweight Example String (in JAVA). Font information in a book. Uses sharing to support large numbers of fine grained objects efficiently. Multiple clients share the immutable part of an object.
6
Construction Patterns
7
Builder Separates construction of a complex object from its representation, so the same process can create different representations. Example JAVA StringBuilder class Parsers that take an input stream then use the information to build an object or representation. Factory Example Object.toString() in the JAVA Object interface The Iterator() method in the JAVA collection interface Threadpools Abstract factory The intent of abstract factory or kit is to allow the creation of families of related or dependent objects, without specifying their concrete classes. Example Creating user interface components with different look & feels by using UIManager and UI delegates. Prototype Provides objects by copying an example Example copy constructors Objects supporting the clone interface. Lets a class developer define an interface for creating an object while retaining control over which class to instantiate. Also lets a class defer initialisation to subclasses. Note A factory is also frequently used to isolate a client from having to manage the reuse of objects. Note JAVA Objects default clone() method produces a shallow copy not a deep copy. You may need to override this method to ensure the objects state information is replicated correctly.
8
Memento Without violating encapsulation, captures and externalizes an objects internal state so it can be restored to this state later. Example REDO and UNDO in applications. Saving and restoring a position in a game.
9
Operation Patterns
10
Template Defines an algorithm in an operation, deferring the definition of some steps so the subclass or other classes can supply them. Example Hooks Sorting collections (you supply the comparator method) State Example Sequencing complex GUIs (multi-input & multi-state) State-machines Strategy Encapsulates different approaches in separate classes that each implement a common operation Example Various sorting algorithms that use different sorting techniques but all implement a method called sort() that achieve the same result. Command Encapsulates a request as an object, allowing a user or service to determine when to evoke it, add parameters to a request, queue or log requests and support undoable operations. Example Menu items that allow an ActionListener to be attached, which implements ActionPerformed() if the menu item is triggered. Note To avoid unnecessary state creation, context usually holds all the state objects, and a context reference is passed to the state object which uses it to obtain the state objects and methods for updating the present state of the context. Note The above UML diagram shows a subclass with an overridden sub-method. Alternatively the sub-method could be passed in using the command design pattern Distributes state specific logic across classes that represent an objects state (encapsulating state logic into separate classes). The object appears to change its class whenever its state changes. Note In some cases a reference to the target is passed in as a parameter when the command is constructed, and is held within the command. The target often has an interface defined on which the command acts to achieve its functionality.
11
Interpreter Lets you compose executable objects according to a set of composition rules. Defines the representation of a language. Defers the creation of executable objects until runtime. Example BASIC, JAVA, CNC machine control programs Note Expressions can be run in a particular context (such as producing results in a particular window)
12
Extension Patterns
13
Decorator Attaches additional responsibilities, fields and method variations to an object at runtime. It is an alternative to sub classing for extending functionality. Example Buffered file writer Filters for streams iterator Example Collection iterators for (class element: collection) loops Composite iterators Visitor Lets you define a new method or methods without changing the classes on which they operate. Example Taxis – you call them, they visit you, then you use their public automobile and your cash to go out to a bar and have some fun. An iterator is an object that sequentially accesses the elements of another aggregated object without exposing its underlying representation. Note In the above UML concrete visitor calls accept on a targetObject. The targetObject then calls the appropriate visit method of the visitor passing a reference to itself. The visitor can then use public fields and methods of the targetObject or its own fields and methods to achieve what is desired.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.