COSC 340: Software Engineering Design Patterns
Michael Jantz
COSC 340: Software Engineering 1
Recommended text: Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides
COSC 340: Software Engineering Design Patterns Michael Jantz - - PowerPoint PPT Presentation
COSC 340: Software Engineering Design Patterns Michael Jantz Recommended text: Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides COSC 340: Software Engineering 1 Why
COSC 340: Software Engineering 1
Recommended text: Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides
COSC 340: Software Engineering 2
COSC 340: Software Engineering 3
COSC 340: Software Engineering 4
COSC 340: Software Engineering 5
Purpose Creational Structural Behavioral Scope Class Factory Method Adapter Interpreter Template Method Object Abstract Factory Builder Prototype Singleton Adapter Bridge Composite Decorator Façade Proxy Chain of Responsibility Command Iterator Mediator Memento Flyweight Observer State Strategy Visitor
6
COSC 340: Software Engineering 7
8
9
COSC 340: Software Engineering 10
COSC 340: Software Engineering 11
COSC 340: Software Engineering 12
COSC 340: Software Engineering 13
COSC 340: Software Engineering 14
COSC 340: Software Engineering 15
COSC 340: Software Engineering 16
COSC 340: Software Engineering 17
COSC 340: Software Engineering 18
COSC 340: Software Engineering 19
20
COSC 340: Software Engineering 21
COSC 340: Software Engineering 22
COSC 340: Software Engineering 23
COSC 340: Software Engineering 24
COSC 340: Software Engineering 25
COSC 340: Software Engineering 26
COSC 340: Software Engineering 27
COSC 340: Software Engineering 28
‒ Declares an interface for operations that create abstract product objects
‒ Implements the operations to create concrete product objects
‒ Declares an interface for a type of product object
‒ Defines a product object to be created by the corresponding concrete factory ‒ Implements the AbstractProduct interface
‒ Uses only interfaces declared by AbstractFactory and AbstractProduct classes
COSC 340: Software Engineering 29
COSC 340: Software Engineering 30
COSC 340: Software Engineering 31
COSC 340: Software Engineering 32
COSC 340: Software Engineering 33
COSC 340: Software Engineering 34
‒ Specify the kinds of objects to create using a prototypical instance, and create new
‒ Useful for when a system needs to be independent of how its objects are created, composed, and represented ‒ Enables you to avoid building a class hierarchy of factories that parallels the class hierarchy of products ‒ Example: graphical application for editing musical scores
‒ Parameterize object creation methods with prototype instance ‒ Create the appropriate objects by copying the prototype
COSC 340: Software Engineering 35
‒ Adding new music objects (notes, rests, staves) ‒ Selecting, moving, manipulating music objects
‒ Abstract graphic class for graphical components ‒ Abstract tool class for defining tools in the palette ‒ GraphicTool class
the graphical objects (notes, staves) are specific to the application
COSC 340: Software Engineering 36
37
COSC 340: Software Engineering 38
COSC 340: Software Engineering 39
COSC 340: Software Engineering 40
41
COSC 340: Software Engineering 42
COSC 340: Software Engineering 43
44
COSC 340: Software Engineering 45
COSC 340: Software Engineering 46
COSC 340: Software Engineering 47
COSC 340: Software Engineering 48
COSC 340: Software Engineering 49
COSC 340: Software Engineering 50
‒ Declares the interface for objects in the composition ‒ Implements default behavior for the interface common to all classes, as appropriate ‒ Declares an interface for accessing and managing its child components ‒ (optional) defines an interface for accessing a component's parent in the recursive structure, and implements it if that's appropriate
‒ Represents leaf objects in the composition (a leaf has no children) ‒ Defines behavior for primitive objects in the composition
‒ Defines behavior for components having children ‒ Stores child components ‒ Implements child-related operations in the Component
‒ Manipulates objects in the composition through the Component interface.
COSC 340: Software Engineering 51
COSC 340: Software Engineering 52
COSC 340: Software Engineering 53
COSC 340: Software Engineering 54
COSC 340: Software Engineering 55
COSC 340: Software Engineering 56
COSC 340: Software Engineering 57
COSC 340: Software Engineering 58
‒ Shape class defines graphical objects (has an editable shape and can draw itself) ‒ Each kind of graphical object is a subclass of shape (LineShape, PolygonShape, etc)
‒ TextView class available from another toolkit, but does not fit the Shape interface ‒ How to use TextView even though our drawing class must conform to a different and incompatible interface? ‒ Solution: create an adapter class that adapts TextView's interface for use with the Drawing Editor program
COSC 340: Software Engineering 59
COSC 340: Software Engineering 60
COSC 340: Software Engineering 61
COSC 340: Software Engineering 62
COSC 340: Software Engineering 63
COSC 340: Software Engineering 64
COSC 340: Software Engineering 65
66
COSC 340: Software Engineering 67
COSC 340: Software Engineering 68
COSC 340: Software Engineering 69
COSC 340: Software Engineering 70
COSC 340: Software Engineering 71
COSC 340: Software Engineering 72
COSC 340: Software Engineering 73
COSC 340: Software Engineering 74
COSC 340: Software Engineering 75
COSC 340: Software Engineering 76
COSC 340: Software Engineering 77
COSC 340: Software Engineering 78
COSC 340: Software Engineering 79
COSC 340: Software Engineering 80
COSC 340: Software Engineering 81
COSC 340: Software Engineering 82
COSC 340: Software Engineering 83
COSC 340: Software Engineering 84
COSC 340: Software Engineering 85
COSC 340: Software Engineering 86
COSC 340: Software Engineering 87
COSC 340: Software Engineering 88
COSC 340: Software Engineering 89
COSC 340: Software Engineering 90
COSC 340: Software Engineering 91
COSC 340: Software Engineering 92
COSC 340: Software Engineering 93
are dependent on the Model component and should be notified when the Model changes
when the state has changed
COSC 340: Software Engineering 94
‒ Knows its observers. Any number of Observer objects may observe a subject ‒ Provides an interface for attaching and detaching Observer objects
‒ Defines an updating interface for objects that should be notified of changes in a subject
‒ Stores state of interest to ConcreteObserver objects ‒ Sends a notification to its observers when its state changes
‒ Maintains a reference to a ConcreteSubject object ‒ Stores state that should stay consistent with the subject's ‒ Implements the Observer updating interface to keep its state consistent with the subject's
COSC 340: Software Engineering 95
COSC 340: Software Engineering 96
COSC 340: Software Engineering 97
98
COSC 340: Software Engineering 99
maintaining the time of day
regular intervals
COSC 340: Software Engineering 100
101
detach the DigitalClock from its subject
the updated hour and minute
COSC 340: Software Engineering 102
COSC 340: Software Engineering 103
COSC 340: Software Engineering 104
105
‒ Aggregate objects should have a way to access their elements without exposing their internal structures ‒ Might also want to traverse the aggregate object in different ways ‒ Or have more than one traversal pending at a time
‒ Responsibility for access and traversal is removed from the aggregate object and put into an iterator object ‒ Iterator class defines an interface for accessing the object's elements ‒ Each iterator object keeps track of the current element
COSC 340: Software Engineering 106
COSC 340: Software Engineering 107
COSC 340: Software Engineering 108
109
COSC 340: Software Engineering 110
COSC 340: Software Engineering 111
COSC 340: Software Engineering 112
COSC 340: Software Engineering 113
COSC 340: Software Engineering 114
COSC 340: Software Engineering 115
COSC 340: Software Engineering 116
COSC 340: Software Engineering 117
118
COSC 340: Software Engineering 119
COSC 340: Software Engineering 120
COSC 340: Software Engineering 121
COSC 340: Software Engineering 122
COSC 340: Software Engineering 123
COSC 340: Software Engineering 124
COSC 340: Software Engineering 125
COSC 340: Software Engineering 126
COSC 340: Software Engineering 127
COSC 340: Software Engineering 128
COSC 340: Software Engineering 129
COSC 340: Software Engineering 131
descend from a particular Tree abstract class
subclasses) and the widget should be flexible enough to do so
COSC 340: Software Engineering 132
COSC 340: Software Engineering 133
COSC 340: Software Engineering 134
COSC 340: Software Engineering 135
elements (tables, figures)
without disturbing other functionality
COSC 340: Software Engineering 136
in multiple contexts simultaneously
COSC 340: Software Engineering 137
COSC 340: Software Engineering 138
COSC 340: Software Engineering 139
COSC 340: Software Engineering 140
COSC 340: Software Engineering 141
COSC 340: Software Engineering 142
‒ Declares an interface through which flyweights can receive and act on extrinsic state.
‒ Implements the Flyweight interface and adds storage for intrinsic state, if any
‒ Not all Flyweight subclasses need to be shared ‒ The Flyweight interface enables sharing, but does not enforce it
‒ Creates and manages flyweight objects; ensures that flyweights are shared properly
‒ Maintains a reference to flyweight(s) ‒ Computes or stores the extrinsic state of flyweight(s)
COSC 340: Software Engineering 143
COSC 340: Software Engineering 144
COSC 340: Software Engineering 145
COSC 340: Software Engineering 146
COSC 340: Software Engineering 147
COSC 340: Software Engineering 148
COSC 340: Software Engineering 149
COSC 340: Software Engineering 150
COSC 340: Software Engineering 151
COSC 340: Software Engineering 152
COSC 340: Software Engineering 153
COSC 340: Software Engineering 154
155
to call ParentClass::Operation
Ensure parent and derived operations are called together
COSC 340: Software Engineering 156