DOC PREVIEW
UMD CMSC 433 - Design Patterns

This preview shows page 1-2-3-4-5-6 out of 19 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CMSC433, Fall 2002 Design PatternsAdam PorterSep 24, 2002CMSC 433, Fall 20022What is a pattern?•Patterns = problem/solution pairs in context•Patterns facilitate reuse of successful software architectures and design•Not code reuse–Instead, solution/strategy reuse–Sometimes, interface reuse2CMSC 433, Fall 20023Gang of Four• The book that started it all•Community refers to authors as the “Gang of Four”•Figures and some text in these slides come from book•On reserve in CS library (3rdfloor AVW)CMSC 433, Fall 20024Object Modeling Technique (OMT)•Used to describe patterns in GO4 book•Graphical representation of OO relationships –Class diagramsshow the static relationship between classes–Object diagramsrepresent the state of a program as series of related objects–Interaction diagramsillustrate execution of the program as an interaction among related objects3CMSC 433, Fall 20025ClassesCMSC 433, Fall 20026Object instantiation4CMSC 433, Fall 20027Subclassingand Abstract ClassesCMSC 433, Fall 20028Pseudo-code and Containment5CMSC 433, Fall 20029Object diagramsCMSC 433, Fall 200210Interaction diagramstime6CMSC 433, Fall 200211Components of a Pattern•Pattern name–identify this pattern; distinguish from other patterns–define terminology•Pattern alias – “also known as”•Real-world example•Context•ProblemCMSC 433, Fall 200212Components of a Pattern (cont’d)•Solution–typically natural language notation•Structure–class (and possibly object) diagram in solution•Interaction diagram (optional)• Consequences–advantages and disadvantages of pattern–ways to address residual design decisions7CMSC 433, Fall 200213Components of a Pattern (cont’d)•Implementation–critical portion of plausible code for pattern•Known uses–often systems that inspired pattern•References -See also–related patterns that may be applied in similar casesCMSC 433, Fall 200214Design patterns taxonomy• Creational patterns –concern the process of object creation•Structural patterns –deal with the composition of classes or objects•Behavioral patterns –characterize the ways in which classes or objects interact and distribute responsibility.8CMSC 433, Fall 200215Creation patterns•Singleton–Ensure a class only has one instance, and provide a global point of access to it.•Typesafe Enum–Generalizes Singleton: ensures a class has a fixed number of unique instances.•Abstract Factory–Provide an interface for creating families of related or dependent objects without specifying their concrete classes. CMSC 433, Fall 200216Structural patterns•Adapter –Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces•Proxy–Provide a surrogate or placeholder for another object to control access to it•Decorator–Attach additional responsibilities to an object dynamically9CMSC 433, Fall 200217Behavioral patterns•Template–Define the skeleton of an algorithm in an operation, deferring some steps to subclasses• State–Allow an object to alter its behavior when its internal state changes. The object will appear to change its class•Observer–Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automaticallyCMSC 433, Fall 200218Principles Underlying Patterns•Rely on abstract classes to hide differences between subclasses from clients–object class vs. object type•class defines how an object is implemented•type defines an object’s interface (protocol)•Program to an interface, not an implementation10CMSC 433, Fall 200219Principles (cont’d)•Black-box vs. white-box reuse–black-box relies on object references, usually through instance variables–white-box reuse by inheritance–black-box reuse preferred for information hiding, run-time flexibility, elimination of implementation dependencies–disadvantages: Run-time efficiency (high number of instances, and communication by message passing)•Favor composition over class inheritanceCMSC 433, Fall 200220Principles (cont’d)•Delegation–powerful technique when coupled with black-box reuse–Allow delegation to different instances at run-time, as long as instances respond to similar messages–disadvantages: •sometimes code harder to read and understand •efficiency (because of black-box reuse)11Some Design PatternsCMSC 433, Fall 200222Singleton objects•Some classes have conceptually one instance–Many printers, but only one print spooler–One file system–One window manager•Naïve: create many objects that represent the same conceptual instance•Better: only create one object and reuse it–Encapsulate the code that manages the reuse12CMSC 433, Fall 200223The Singleton solution• Class is responsible for tracking its sole instance–Make constructor private–Provide static method/field to allow access to the only instance of the class•Benefit:–Reuse implies better performance–Class encapsulates code to ensure reuse of the object; no need to burden clientCMSC 433, Fall 200224Singleton pattern13CMSC 433, Fall 200225Implementing the Singleton method•In Java, just define a final static fieldpublic class Singleton {private Singleton() {…}final private static Singleton instance = new Singleton();public Singleton getInstance() { return instance; }}•Java semantics guarantee object is created immediately before first useCMSC 433, Fall 200226Generalizing Singleton: Typesafe Enum•Problem:–Need a number of unique objects, not just one–Basically want a C-style enumerated type, but safe•Solution:–Generalize the Singleton Pattern to keep track of multiple, unique objects (rather than just one)14CMSC 433, Fall 200227Typesafe Enum PatternEnumstatic Enum inst1static Enum inst2EnumOp ()EnumEnumEnumOp ()EnumOp ()datadatadataNote: constructor is privateCMSC 433, Fall 200228Typesafe Enum: Examplepublic class Suit {private final String name;private Suit(String name) { this.name = name; }public String toString() { return name; }public static final Suit CLUBS = new Suit(“clubs”);public static final Suit DIAMONDS = new Suit(“diamonds”);public static final Suit HEARTS = new Suit(“hearts”);public static final Suit SPADES = new Suit(“spades”);}15CMSC 433, Fall 200229Adapter Motivation•Situation:–You have some code you want to use for a program–You can’t


View Full Document

UMD CMSC 433 - Design Patterns

Documents in this Course
Trace 1

Trace 1

62 pages

Reflection

Reflection

137 pages

Testing

Testing

25 pages

Paradigms

Paradigms

10 pages

Testing

Testing

17 pages

Java RMI

Java RMI

17 pages

Java RMI

Java RMI

17 pages

Java RMI

Java RMI

17 pages

Trace 1

Trace 1

46 pages

Jini

Jini

4 pages

Final

Final

15 pages

Java RMI

Java RMI

13 pages

Testing

Testing

16 pages

Load more
Download Design Patterns
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Design Patterns and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Design Patterns 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?