Download presentation
Presentation is loading. Please wait.
1
Spring 2010CS 2251 Design Patterns
2
Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software design" (Wikipedia) "descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context" (Gamma et al.)
3
Spring 2010CS 2253 Examples from Other Fields Architecture: You can build many houses from the same floor plan without having any that are identical. Creative Writing: –Macbeth and Hamlet are both variations of the "tragically flawed hero" story –Genre fiction (e.g. romances) are based on a common sequence of plot elements.
4
Spring 2010CS 2254 Simple Programming Examples Console menu: programs which are based on a loop which waits for a user to select from a limited number of choices and acts on that selection Maintaining a collection: Two-person game:
5
Spring 2010CS 2255 Elements of a design pattern Pattern name Problem for which the pattern provides a solution Solution - elements that make up the design –relationships –responsibilities –collaborations Consequences
6
Spring 2010CS 2256 Pattern Categories Creational –Factory Method –Singleton Structural –Composite –Decorator Behavioral –Iterator –Strategy –Observer
7
Spring 2010CS 2257 Iterator Problem: a client class need access to individual elements of an aggregate object Solution: Define an iterator class that steps through the elements one at a time and remembers where it is in the sequence.
8
Spring 2010CS 2258 Factory Method Allows instantiation of an object to be deferred to a subclass –iterator method of a collection class is a factory method
9
Spring 2010CS 2259 Components and Containers Components are grouped together into containers Containers can also be added to other containers –Containers need to be components as well Composite pattern provides a solution
10
Spring 2010CS 22510 Composite Combines several objects into an object with the same behavior as the parts
11
Spring 2010CS 22511 Layout Managers Consider the problem of organizing components within a container –Different layout managers use different rules for how to organize the components This is an example of the strategy pattern
12
Spring 2010CS 22512 Strategy Define a family of algorithms which are interchangeable Uses –Allows one class to have different behaviors –Allow the use of variants of an algorithm –Allow algorithms to hide data that client doesn't need to see
13
Spring 2010CS 22513 Decorator Enhances the behavior of a single component –e.g. scroll bars
14
Spring 2010CS 22514 Singleton Used to insure that a class only has one instance The one object should be globally accessible
15
Spring 2010CS 22515 Command Encapsulate a request as an object
16
Spring 2010CS 22516 Frameworks Framework is a set of cooperating classes that implement mechanisms essential to a particular problem domain Frameworks can be customized to a particular application within the domain Java has a collections framework
17
Spring 2010CS 22517 Model-View-Controller Architecture Consider a program that shows you two different views of the same data MVC architecture has three parts –Model - the data, not visible –View - a visual representation of the data –Controller - Processes user interaction Observer pattern is used for the view(s)
18
Spring 2010CS 22518 Observer Pattern Define a one-to-many dependency between objects Changes in the one object are reflected in all the dependent objects Used to maintain consistency between objects
19
Spring 2010CS 22519 Visitor Design Pattern This is a common way of handling the process of performing an action on each element of a collection The Visitor interface has a single method –visit( E element) The collection class can have traversal methods that take a Visitor as a parameter
20
Spring 2010CS 22520 Adaptor You have a class that has all the functionality required by a client but doesn't match the required interface
21
Spring 2010CS 22521 Antipatterns Designs that should be avoided –Blob - a class with many unrelated responsibilities –Poltergeist - creates short-lived objects with no significant responsibilities
22
Spring 2010CS 22522 References Design Patterns: Elements of reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides Object-Oriented Design and Patterns by Cay Horstman http://hillside.net/patterns
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.