Object-Oriented Design Patterns

Transcription

CSC 335: Object-OrientedProgramming and DesignObject-OrientedDesign Patterns

OutlineOverview of Design PatternsFour Design ver

The Beginning of PatternsChristopher Alexander, architect– A Pattern Language--Towns, Buildings, Construction– Timeless Way of Building (1979)– “Each pattern describes a problem which occurs overand over again in our environment, and then describesthe core of the solution to that problem, in such a waythat you can use this solution a million times over,without ever doing it the same way twice.”Other patterns: novels (tragic, romantic, crime),movies genres (drama, comedy, documentary)

“Gang of Four” (GoF) BookDesign Patterns: Elements of Reusable Object-OrientedSoftware, Addison-Wesley Publishing Company, 1994Written by this "gang of four"– Dr. Erich Gamma, then Software Engineer, Taligent, Inc.– Dr. Richard Helm, then Senior Technology Consultant, DMR Group– Dr. Ralph Johnson, then and now at University of Illinois, ComputerScience Department– Dr. John Vlissides, then a researcher at IBM Thomas J. Watson Research Center See John's WikiWiki tribute page http://c2.com/cgi/wiki?JohnVlissides

Object-Oriented Design PatternsThis book defined 23 patterns in three categories– Creational patterns deal with the process of object creation– Structural patterns, deal primarily with the static composition andstructure of classes and objects– Behavioral patterns, which deal primarily with dynamic interactionamong classes and objects

Documenting Discovered PatternsMany other patterns have been introduceddocumented– For example, the book Data Access Patterns by CliftonNock introduces 4 decoupling patterns, 5 resource patterns,5 I/O patterns, 7 cache patterns, and 4 concurrency patterns.– Other pattern languages include telecommunicationspatterns, pedagogical patterns, analysis patterns– Patterns are mined at places like Patterns Conferences

ChiliPLoPRecent patterns books work shopped atChiliPLoP, Wickenburg and Carefree Arizona––––Patterns of Enterprise Application Arhitecture Martin FowlerPatterns of Fault Tolerant Software, Bob HamnerPatterns in XML Fabio ArciniegasPatterns of Adopting Agile Development Practices AmrElssamadisy– 2010: Patterns of Parallel Programming, Ralph Johnson 16 patterns and one Pattern Language work shopped

GoF Patterns– Creational Patterns Abstract FactoryBuilderFactory MethodPrototypeSingleton– Structural Patterns xy– Behavioral Patterns Chain of Responsibility Command Interpreter Iterator MediatorMementoObserverState Strategy Template Method Visitor

Why Study Patterns?Reuse tried, proven solutions– Provides a head start– Avoids gotchas later (unanticipated things)– No need to reinvent the wheelEstablish common terminology– Design patterns provide a common point of reference– Easier to say, “We could use Strategy here.”Provide a higher level prospective– Frees us from dealing with the details too early

Other advantagesMost design patterns make software moremodifiable, less brittle– we are using time tested solutionsUsing design patterns makes software systemseasier to change—more maintainableHelps increase the understanding of basic objectoriented design principles– encapsulation, inheritance, interfaces, polymorphism

Style for Describing PatternsWe will use this structure:– Pattern name– Recurring problem: what problem the patternaddresses– Solution: the general approach of the pattern– UML for the pattern Participants: a description as a class diagram– Use Example(s): examples of this pattern, in Java

A few OO Design PatternsComing up:– Iterator access the elements of an aggregate objectsequentially without exposing its underlyingrepresentation– Strategy A means to define a family of algorithms, encapsulateeach one as an object, and make them interchangeable– Observer a preview One object stores a list of observers that are updatedwhen the state of the object is changed

Iterator

Pattern: IteratorName: Iterator (a.k.a Enumeration)Recurring Problem: How can you loop over all objectsin any collection. You don’t want to change client codewhen the collection changes. Want the same methodsSolution: 1) Have each class implement an interface,and 2) Have an interface that works with all collectionsConsequences: Can change collection class detailswithout changing code to traverse the collection

GoF Versionof Iterator page / A C ImplementationListIterator Employee itr list.iterator();for(itr.First(); !itr.IsDone(); itr.Next()) {cout itr.CurrentItem().toString();

Java version of Iteratorinterface Iteratorboolean hasNext()Returns true if the iteration has more elements.Object next()Returns the next element in the iteration and updates the iteration torefer to the next (or have hasNext() return false)void remove()Removes the most recently visited element

Java’s Iterator interface// The Client codeList BankAccount bank new ArrayList BankAccount ();bank.add(new BankAccount("One", 0.01) );// .bank.add(new BankAccount("Nine thousand", 9000.00));String ID "Two";Iterator BankAccount itr bank.iterator();while(itr.hasNext()) System.out.println("Found " ref.getID());}

UML Diagram of Java'sIterator with a few Collections interface interface Listiterator(): Iterator eratorhasNext()next()

Decorator Design PatternRick MercerCSC 335: Object-OrientedProgramming and Design

The Decorator Patternfrom GoFIntent– Attach additional responsibilities to an object dynamically.Decorators provide a flexible alternative to sub classing toextend flexibilityAlso Known As WrapperMotivation– Want to add properties to an existing object.2 Examples Add borders or scrollbars to a GUI component Add stream functionality such as reading a line of input orcompressing a file before sending it over the wire

ApplicabilityUse Decorator– To add responsibilities to individual objectsdynamically without affecting other objects– When extending classes is impractical Sometimes a large number of independent extensionsare possible and would produce an explosion ofsubclasses to support every combination (thisinheritance approach is on the next few slides)

An ApplicationSuppose there is a TextView GUI componentand you want to add different kinds of bordersand/or scrollbars to itYou can add 3 types of borders– Plain, 3D, Fancyand 1 or 2 two scrollbars– Horizontal and VerticalAn inheritance solution requires15 classes forone view

That’s a lot of classes!1.TextView Plain2.TextView Fancy3.TextView 3D4.TextView Horizontal5.TextView Vertical6.TextView Horizontal Vertical7.TextView Plain Horizontal8.TextView Plain Vertical9.TextView Plain Horizontal Vertical10.TextView 3D Horizontal11.TextView 3D Vertical12.TextView 3D Horizontal Vertical13.TextView Fancy Horizontal14.TextView Fancy Vertical15.TextView Fancy Horizontal Vertical

DisadvantagesInheritance solution has an explosion of classesIf another view were added such as StreamedVideoView,double the number of Borders/Scrollbar classesSolution to this explosion of classes?– Use the Decorator Pattern instead

ize()1Decorator contains avisual componentAn aw()resize()Horizdraw()resize()Vertdraw()resize()

Decorator's General Form

JScrollPaneAny Component such as Container, JList,Panel can be decorated with a JScrollPaneThe next slide shows how to decorate a JPanelwith a JScrollPane

Decorate a JPanelJScrollPane scrollPane new JScrollPane(toStringView);add(scrollPane); // Add to a JFrame or another panel

Motivation ContinuedThe more flexible containment approach encloses thecomponent in another object that adds the borderThe enclosing object is called the decoratorThe decorator conforms to the interface of thecomponent so its presence is transparent to clientsThe decorator forwards requests to the component andmay perform additional actions before or after anyforwarding

Decorator Design: Java StreamsInputStreamReader(InputStream in)System.in is an InputStream object– . bridge from byte streams to character streams: It reads bytesand translates them into characters using the specified characterencoding. JavaTMAPIBufferedReader– Read text from a character-input stream, buffering characters so asto provide for the efficient reading of characters, arrays, and lines.JavaTMAPIWhat we had to do for console input before Java 1.5’s ScannerBufferedReader keyboard new BufferedReader(newInputStreamReader(System.in));

Decorator pattern in the real worldBufferedReader decorates InputStreamReaderBufferedReaderreadLine() // add a useful methodInputStreamReaderread()// 1 byte at a timeclose()Still needed to parse integers, doubles, or words

Java streamsWith 60 streams in Java, you can create a widevariety of input and output streams– this provides flexibility good it also adds complexity– Flexibility made possible with inheritance and classesthat accept classes that extend the parameter type

Another Decorator ExampleWe decorated a FileInputStream with anObjectInputStream to read objects thatimplement Serializable– and we used FileOutputStream withObjectOutputStream– then we were able to use nice methods like these tworead and write large complex objects on the file system:\outFile.writeObject(list);// and later on list (ArrayList String )inFile.readObject();

Another Decorator ExampleRead a plain text file and compress it using theGZIP format ZIP.javaRead a compress file in the GZIP format and writeit to a plain text file UNGZIP.javaSample text iliad10.txt from Project Gutenbergbytes875,736 iliad10.txt bytes305,152 iliad10.gz875,736 TheIliadByHomer(after code on next slide)

// Open the input fileString inFilename "iliad10.txt";FileInputStream input new FileInputStream(inFilename);// Open the output fileString outFilename "iliad10.gz";GZIPOutputStream out new GZIPOutputStream(new FileOutputStream(outFilename));// Transfer bytes from output file to compressed filebyte[] buf new byte[1024];int len;while ((len input.read(buf)) 0) {out.write(buf, 0, len);}// Close the file and streaminput.close();out.close();

// Open the gzip fileString inFilename "iliad10.gz";GZIPInputStream gzipInputStream new GZIPInputStream(new FileInputStream(inFilename));// Open the output fileString outFilename "TheIliadByHomer";OutputStream out new FileOutputStream(outFilename);// Transfer bytes from compressed file to output filebyte[] buf new byte[1024];int len;while ((len gzipInputStream.read(buf)) 0) {out.write(buf, 0, len);}// Close the file and streamgzipInputStream.close();out.close();

GZIPInputStream is a DecoratorGZIPInputStream

SummaryDecorators are very flexible alternative ofinheritanceDecorators enhance (or in some cases restrict)the functionality of decorated objectsThey work dynamically to extend classresponsibilities, even inheritance does some butin a static fashion at compile time

Strategy Design PatternStrategy

Pattern: StrategyName: Strategy (a.k.a Policy)Problem: You want to encapsulate a family ofalgorithms and make them interchangeable.Strategy lets the algorithm vary independentlyfrom the clients that use it (GoF)Solution: Create an abstract strategy class (orinterface) and extend (or implement) it innumerous ways. Each subclass defines thesame method names in different ways

Design Pattern: StrategyConsequences:– Allows families of algorithmsKnown uses:–––––Critters seen in section for Rick’s 127B / 227Layout managers in JavaDifferent Poker Strategies in a 335 ProjectDifferent PacMan chase strategies in a 335 ProjectDifferent Jukebox policies that can be

Java Example of Strategythis.setLayout(new FlowLayout());this.setLayout(new GridLayout());In Java, a container HAS-A layout manager– There is a default– You can change a container's layout manager witha setLayout message

Change the stategy at runtime Demonstrate LayoutControllerFrame.javaprivate class FlowListenerimplements ActionListener {// There is another ActionListener for GridLayoutpublic void actionPerformed(ActionEvent evt) {// Change the layout strategy of the JPanel// and tell it to lay itself outcenterPanel.setLayout(new FlowLayout());centerPanel.validate();}}12-43

interface LayoutManager– Java has interface java.awt.LayoutManager– Known Implementing Classes GridLayout, FlowLayout, ScrollPaneLayout– Each class implements the following methodsaddLayoutComponent(String name, Component comp)layoutContainer(Container parent)minimumLayoutSize(Container parent)preferredLayoutSize(Container parent)removeLayoutComponent(Component comp)

UML Diagram of StrategyGeneral FormContextstrategy: Strategy interface StrategyAlgorithmInterfacesetStrategy(Strategy) erface

Specific UML Diagram ofLayoutManager in Java interface JPanellayoutMan: LayoutManagersize: DimensionsetLayout(lm: tainer()minimumLayoutSize()

Another Example– Pac Man GhostChasesPacMan strategies in 2001– Level 1: random– Level 2: a bit smarter– Level 3: use a shortest path – Could be interface ChaseStategy is in the Ghost classinterface ChaseStategy {public Point nextPointToMoveTo();}– and Ghost has setChaseStrategy(new ShortestPath())

The Observer Design PatternName: ObserverProblem: Need to notify a changing number ofobjects that something has changedSolution: Define a one-to-many dependencybetween objects so that when one objectchanges state, all its dependents are notifiedand updated automatically

ExamplesFrom Heads-First: Send a newspaper to all whosubscribe– People add and drop subscriptions, when a newversion comes out, it goes to all currently describedSpreadsheet– Demo: Draw two charts—two views--with somechanging numbers--the model16-49

ExamplesFile Explorer (or Finders) are registeredobservers (the view) of the file system (themodel).Demo: Open several finders to view file systemand delete a fileLater in Java: We'll have two views of the samemodel that get an update message whenever thestate of the model has changed16-50

Observer Example

GZIP format ZIP.java Read a compress file in the GZIP format and write it to a plain text file UNGZIP.java Sample text iliad10.txt from Project Gutenberg bytes 875,736 iliad10.txt bytes 305,152 ilia