DOC PREVIEW
UNM CS 580 - Design Patterns

This preview shows page 1-2-15-16-31-32 out of 32 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 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 32 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 32 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 32 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 32 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 32 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1Design PatternsCS 580By Robert StehwienRecommended Readingand Lecture Sources• “Design Patterns: Elements of Reusable Object-Oriented Software” by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides• “Refactoring to Patterns” by Joshua Kerievsky– http://www.industriallogic.com• “Head First Design Patterns” by Eric Freeman & Elisabeth Freeman with Kathy Sierra & Bert Bates• “Object-Oriented Design Heuristics” by Arthur J. Riel2What’s a pattern?• A pattern is a named abstraction from a concrete form that represents a recurring solution to a particular problem. • Patterns = problem/solution pairs in context• Patterns facilitate reuse of successful software architectures and design• Not code reuse– Instead, solution/strategy reuse– Sometimes, interface reuse• Best practices• “Patterns are discovered, not invented” - Richard HelmPatterns and Language• Most design patterns focus on OO– Assume inheritance, polymorphism, encapsulation, etc.• In procedural languages, might add OO features as “patterns”• Some languages provide or make it easier to implement some patterns– http://www.c2.com/cgi/wiki?AreDesignPatternsMissingLanguageFeatures• Covering generic OO patterns not concurrency, domain specific, UI, etc.3Categories of Patterns• Design patterns vary in granularity and level of abstraction.• By categorizing patterns, it becomes easier to recognize and learn them• Categorized by purpose or scope• Purpose reflects what the pattern does• Scope specifies if the pattern applies to classes or objectsPurpose• Reflects what the pattern does• Creational – facilitate object creation• Structural -- deal with the composition of classes or objects• Behavioral -- deal with the way objects interact and distribute responsibility4Scope• Whether the pattern applies to classes or objects• Class Patterns:– Deal with relationships between classes or subclasses established through inheritance– These relationships are fixed at compile time• Objects patterns: – Deal with object relationships that can be changed at runtimeOther Ways to Organize Patterns• Some patterns are frequently used with other patterns, for example composite is often used with visitor and/or iterator• Some patterns are alternatives: Prototype is usually an alternative for Abstract Factory• Some patterns are very similar in structure, such as Composite and Decorator5How do Design Patterns Solve Design Problems?• The meaning of OO is well-known• The hard part is decomposing the system into objects to exploit:– encapsulation– flexibility, extensibility– ease of modification– performance– evolution– reusabilityFinding Objects• Many objects in the design come from the analysis model• But OO designs often end up with classes that have no counterpart in the real world • Design Patterns help the modeler to identify less obvious abstractions and the objects that can capture them.6Inheritance• Inheritance is a mechanism for reuse• You can define a new kind of object in terms of the old one• You get new implementations almost for free, inheriting most of what you need from an existing class• This is only half the story!Program to an interface,not an implementation• Inheritance permits defining a family of objects with identical interfaces• Polymorphism depends on this!• All derived classes share the base class interface• Subclasses add or override operations, rather than hiding operations• All subclasses then respond to requests in the interface of the abstract class7Inheritance properly used• Clients are unaware of types of objects: heuristic 5.12 - “explicit case analysis on the type of an object is usually an error. The designer should use polymorphism”. Riel• Clients only know about the abstract classes defining the interface. heuristic 5.7: “All base classes should be abstract”. Riel• This reduces implementation dependencies • Program to an interface, not an implementation• Creational patterns help ensure this rule!Two techniques for reuse• Inheritance: White box• Object Composition: Black box8Advantage of Inheritance for Reuse• Defined statically• Supported directly by the language: straightforward to use• Easier to modify implementation being reusedDisadvantage of Inheritance• Can’t change inherited implementation at run-time• Parent classes often define part of their subclasses physical representation• Because inheritance exposes the parent implementation it’s said to “break encapsulation” (GOF p.19, Sny86)• Change in parent => Change in subclass• If inherited attribute is inappropriate?9Duck ExampleDuck demo courtesy of “Head First Design Patterns”Extending Duck• Static ducks not enough to compete• Ducks need to fly()• Inheritance seems like a good way to go10Flying Duck 1Sadly Rubber Duckies• Don’t quack(); already overidden to squeak• Don’t fly at all; overide to do nothing?• What about DecoyDucks– They don’t quack or fly11Decoy and Rubber DucksWhat about Interfaces?12Object Composition:Black Box Reuse• Objects are accessed solely through their interfaces: no break of encapsulation• Any object can be replaced by another at runtime: as long as they are the same type• Favor object composition over class inheritance. GOF p. 20• But inheritance & composition work together!Delegation• “a way of making composition as powerful for reuse as inheritance”:– GOF, p. 20– [JZ91] Ralph Johnson and Jonathan Zweig, Delegation in C++, JOOP, f(11):22-35, Nov. 91– [Lie86] Henry Lieberman. Using prototypical objects to implement shared behavior in OO systems. OOPSLA, pp 214-223, Nov. ‘8613Delegation: Reuse• In delegation, 2 objects handle a request• Analogous to subclass deferring request to parent• In delegation, the receiver passes itself to the delegate to let the delegated operation refer to the receiver!Delegating area() to RectangleWindowarea()Rectanglerectanglearea()return rectangle->area()return width*heightwidthheight14Advantages of Delegation• Can change behaviors at run-time• Window can become circular at run-time by replacing Rectangle with Circle, (assuming Rectangle & Circle are same type!)• There are run-time costs.• Delegation in patterns: State, Strategy, Visitor, Mediator, BridgeRedesigned Duck15Encapsulated Quack BehaviorEncapsulated Fly Behavior16Strategy Pattern• Surprise! That is the


View Full Document

UNM CS 580 - Design Patterns

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?