Unformatted text preview:

Lecture 13: Design PatternsKenneth M. AndersonObject-Oriented Analysis and DesignCSCI 6448 - Spring Semester, 20051February 22, 2005 © University of Colorado, Boulder, 20052Pattern ResourcesPattern Languages of ProgrammingTechnical conference on PatternsThe Portland Pattern Repositoryhttp://c2.com/ppr/Patterns Homepagehttp://hillside.net/Go to page then click on “Patterns tab”February 22, 2005 © University of Colorado, Boulder, 20053Design PatternsAddison-Wesley book published in 1995Erich GammaRichard HelmRalph JohnsonJohn VlissidesISBN 0-201-63361-2Known as “The Gang of Four”Presents 23 Design PatternsMaterial in this lecture and lecture 26 is drawn from this book, and is thus copyright © 1995 by Addison-Wesley Publishing CompanyFebruary 22, 2005 © University of Colorado, Boulder, 20054What are Patterns?Christopher Alexander talking about buildings and towns“Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice”Alexander, et al., A Pattern Language. Oxford University Press, 1977February 22, 2005 © University of Colorado, Boulder, 20055Patterns, continuedPatterns can have different levels of abstractionIn Design Patterns (the book),Patterns are not classesPatterns are not frameworksInstead, Patterns are descriptions of communicating objects and classes that are customized to solve a general design problem in a particular contextFebruary 22, 2005 © University of Colorado, Boulder, 20056Patterns, continuedSo, patterns are formalized solutions to design problemsThey describe techniques for maximizing flexibility, extensibility, abstraction, etc.These solutions can typically be translated to code in a straightforward mannerFebruary 22, 2005 © University of Colorado, Boulder, 20057Elements of a PatternPattern NameMore than just a handle for referring to the patternEach name adds to a designer’s vocabularyEnables the discussion of design at a higher abstractionThe ProblemGives a detailed description of the problem addressed by the patternDescribes when to apply a patternOften with a list of preconditionsFebruary 22, 2005 © University of Colorado, Boulder, 20058Elements of a Pattern, continuedThe SolutionDescribes the elements that make up the design, their relationships, responsibilities, and collaborationsDoes not describe a concrete solutionInstead a template to be applied in many situationsFebruary 22, 2005 © University of Colorado, Boulder, 20059Elements of a Pattern, continuedThe consequencesDescribes the results and tradeoffs of applying the patternCritical for evaluating design alternativesTypically includeImpact on flexibility, extensibility, or portabilitySpace and Time tradeoffsLanguage and Implementation issuesFebruary 22, 2005 © University of Colorado, Boulder, 200510Design Pattern TemplatePattern Name and ClassificationCreationalStructuralBehavioralIntentAlso Known AsMotivation and ApplicabilityStructure and ParticipantsCollaborationsConsequencesImplementationSample CodeKnown UsesRelated PatternsFebruary 22, 2005 © University of Colorado, Boulder, 200511ExamplesSingletonFactory MethodAdapterFebruary 22, 2005 © University of Colorado, Boulder, 200512SingletonIntentEnsure a class has only one instance, and provide a global point of access to itMotivationSome classes represent objects where multiple instances do not make sense or can lead to a security risk (e.g. Java security managers)February 22, 2005 © University of Colorado, Boulder, 200513Singleton, continuedApplicabilityUse the Singleton pattern whenthere must be exactly one instance of a class, and it must be accessible to clients from a well-known access pointwhen the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their codeFebruary 22, 2005 © University of Colorado, Boulder, 200514Singleton StructureSingletonstatic Instance()public SingletonOperation()public GetSingletonData()private static uniqueInstanceprivate singletonData{return uniqueInstance}February 22, 2005 © University of Colorado, Boulder, 200515Singleton, continuedParticipantsJust the Singleton classCollaborationsClients access a Singleton instance solely through Singleton’s Instance operationConsequencesControlled access to sole instanceReduced name space (versus global variables)Permits a variable number of instances (if desired)February 22, 2005 © University of Colorado, Boulder, 200516Implementationimport java.util.Date;public class Singleton { private static Singleton theOnlyOne; private Date d = new Date(); private Singleton() { } public synchronized static Singleton instance() { if (theOnlyOne == null) { theOnlyOne = new Singleton(); } return theOnlyOne; } public Date getDate() { return d; }}February 22, 2005 © University of Colorado, Boulder, 200517Using our Singleton Classpublic class useSingleton { public static void main(String[] args) { Singleton a = Singleton.instance(); Singleton b = Singleton.instance(); System.out.println("" + a.getDate()); System.out.println("" + b.getDate()); System.out.println("" + a); System.out.println("" + b); }}Output:Sun Apr 07 13:03:34 MDT 2002Sun Apr 07 13:03:34 MDT 2002Singleton@136646Singleton@136646February 22, 2005 © University of Colorado, Boulder, 200518Names of Classes in PatternsAre the class names specified in a pattern required?No!Consider an environment where a system has access to only one printerWould you want to name the class that provides access to the printer “Singleton”??!!No, you would want to name it something like “Printer”!On the other handIncorporating the names of a pattern’s roles can help to communicate their use to designers“Oh, I see you have a “PrinterObserver” class, are you using the Observable design pattern?February 22, 2005 © University of Colorado, Boulder, 200519Names, continuedSo, if names are unimportant, what is?Structure!We can name our Singleton class anything so long as ithas a private or protected constructorneed a protected constructor to allow subclasseshas a static “instance” operation to retrieve the single instanceFebruary 22, 2005 © University of Colorado, Boulder, 200520Factory MethodIntentDefine an interface for creating an


View Full Document

CU-Boulder CSCI 6448 - Design Patterns

Documents in this Course
Struts

Struts

12 pages

Adapter

Adapter

23 pages

Prototype

Prototype

16 pages

Weka

Weka

15 pages

qooxdoo

qooxdoo

16 pages

Django

Django

12 pages

Overview

Overview

22 pages

XNA

XNA

5 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?