New version page

UMD CMSC 132 - Design Patterns I

Documents in this Course
Recursion

Recursion

12 pages

Sorting

Sorting

31 pages

HTML

HTML

7 pages

Trees

Trees

19 pages

HTML

HTML

18 pages

Trees

Trees

19 pages

Honors

Honors

19 pages

Lecture 1

Lecture 1

11 pages

Quiz #3

Quiz #3

2 pages

Hashing

Hashing

21 pages

Load more
Upgrade to remove ads

This preview shows page 1-2-3-26-27-28 out of 28 pages.

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

Upgrade to remove ads
Unformatted text preview:

1CMSC 132: Object-Oriented Programming IIDesign Patterns IDepartment of Computer ScienceUniversity of Maryland, College Park2Design PatternsDescriptions of reusable solutions to common software design problemsCaptures the experience of expertsRationale for designTradeoffs Codifies design in reusable formExampleIterator pattern3GoalsSolve common programming challengesImprove reliability of solutionAid rapid software developmentUseful for real-world applications4ObservationsDesign patterns are like recipes – generic solutions to expected situationsDesign patterns are language independentRecognizing when and where to use design patterns requires familiarity & experienceDesign pattern libraries serve as a glossary of idioms for understanding common, but complex solutions5Observations (cont.)Many design patterns may need to fit togetherDesign Patterns (by Gamma et al. 1995, a.k.a. Gang of Four, or GOF) list 23 design patternsAround 250 common OO design patternsDesign patterns are used throughout the Java Class Libraries6Documentation Format1.Motivation or context for pattern2.Prerequisites for using a pattern3.Description of program structure 4.List of participants (classes & objects)5.Collaborations (interactions) between participants6.Consequences of using pattern (good & bad)7.Implementation techniques & issues8.Example codes9.Known uses 10.Related patterns7Types of Design PatternsCreationalDeal with the best way to create objects StructuralWays to bring together groups of objectsBehavioral Ways for objects to communicate & interact8Creational Patterns1.Abstract Factory- Creates an instance of several families of classes2.Builder - Separates object construction from its representation3.Factory Method - Creates an instance of several derived classes4.Prototype - A fully initialized instance to be copied or cloned5.Singleton - A class of which only a single instance can exist9Structural Patterns6.Adapter - Match interfaces of different classes7.Bridge - Separates an object’s interface from its implementation8.Composite - A tree structure of simple and composite objects9.Decorator - Add responsibilities to objects dynamically10.Façade - Single class that represents an entire subsystem11.Flyweight - Fine-grained instance used for efficient sharing12.Proxy - Object representing another object10Behavioral Patterns13.Chain of Responsibility - A way of passing a request between a chain of objects14.Command - Encapsulate a command request as an object15.Interpreter - A way to include language elements in a program16.Iterator - Sequentially access the elements of a collection17.Mediator - Defines simplified communication between classes18.Memento - Capture and restore an object's internal state11Behavioral Patterns (cont.)19.Observer - A way of notifying change to a number of classes20.State - Alter an object's behavior when its state changes21.Strategy - Encapsulates an algorithm inside a class22.Template Method - Defer the exact steps of an algorithm to a subclass23.Visitor - Defines a new operation to a class without changing class12Iterator PatternDefinitionMove through list of objects without knowing its internal representationWhere to use & benefitsUse a standard interface to represent data objectsUses standard iterator built in each standard collection, like List, Sort, or MapNeed to distinguish variations in the traversal of an aggregate13Iterator PatternExampleIterator for collectionOriginalExamine elements of collection directlyUsing patternCollection provides Iterator class for examining elements in collection14Iterator Examplepublic interface Iterator<V> {bool hasNext();V next();} Iterator<V> it = myCollection.iterator();while ( it.hasNext() ) {V x = it.next(); // finds all objects… // in collection}15Singleton PatternDefinitionOne instance of a class or value accessible globallyWhere to use & benefitsEnsure unique instance by defining class finalAccess to the instance only via methods provided16Singleton Examplepublic class Employee {public static final int ID = 1234; // ID is a singleton} public final class MySingleton {// declare the unique instance of the classprivate static MySingleton uniq = new MySingleton();// private constructor only accessed from this classprivate MySingleton() { … }// return reference to unique instance of classpublic static MySingleton getInstance() {return uniq;}}17Adapter PatternDefinitionConvert existing interfaces to new interfaceWhere to use & benefitsHelp match an interfaceMake unrelated classes work togetherIncrease transparency of classes18Adapter PatternExampleAdapter from integer Set to integer Priority QueueOriginalInteger set does not support Priority Queue Using patternAdapter provides interface for using Set as Priority QueueAdd needed functionality in Adapter methods19Adapter Examplepublic interface PriorityQueue { // Priority Queuevoid add(Object o);int size();Object removeSmallest();}20Adapter Examplepublic class PriorityQueueAdapter implements PriorityQueue {Set s;PriorityQueueAdapter(Set s) { this.s = s; }public void add(Object o) { s.add(o); }int size() { return s.size(); }public Integer removeSmallest() {Integer smallest = Integer.MAX_VALUE;Iterator it = s.iterator();while ( it.hasNext() ) {Integer i = it.next();if (i.compareTo(smallest) < 0)smallest = i;}s.remove(smallest);return smallest;}}21Factory PatternDefinitionProvides an abstraction for deciding which class should be instantiated based on parameters givenWhere to use & benefitsA class cannot anticipate which subclasses must be createdSeparate a family of objects using shared interfaceHide concrete classes from the client22Factory PatternExampleCar Factory produces different Car objectsOriginalDifferent classes implement Car interfaceDirectly instantiate car objectsNeed to modify client to change carsUsing patternUse carFactory class to produce car objectsCan change cars by changing carFactory23Factory Exampleclass 350Z implements Car; // fast carclass Ram implements Car; // truckclass Accord implements Car; // family carCar fast = new 350Z(); // returns fast carpublic class carFactory {public static Car create(String type) {if (type.equals("fast")) return new 350Z();if (type.equals("truck")) return new Ram();else if (type.equals(“family”) return new Accord();}}Car fast = carFactory.create("fast"); // returns fast car24Decorator PatternDefinitionAttach additional responsibilities or functions to an object dynamically or staticallyWhere to use &


View Full Document
Download Design Patterns I
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 I 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 I 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?