Download presentation
Presentation is loading. Please wait.
1
(c) 2010 University of California, Irvine – André van der Hoek1July 16, 2015 – 13:45:31 Informatics 122 Software Design II Lecture 8 Nick Lopez Duplication of course material for any commercial purpose without the explicit written permission of the professor is prohibited.
2
(c) 2010 University of California, Irvine – André van der Hoek2July 16, 2015 – 13:45:31 Today’s Lecture Design patterns Assignment 4
3
(c) 2010 University of California, Irvine – André van der Hoek3July 16, 2015 – 13:45:31 Fundamental Principles Apply rigor Separate concerns –modularize –abstract Anticipate change Generalize Work incrementally
4
(c) 2010 University of California, Irvine – André van der Hoek4July 16, 2015 – 13:45:31 A Checklist on Overall Design Strive for grouping related functionality (high cohesion) Strive for ungrouping semi-related functionality (high cohesion) Strive for reducing interdependency (low coupling)
5
(c) 2010 University of California, Irvine – André van der Hoek5July 16, 2015 – 13:45:31 A Checklist on Class Design Cohesion Completeness Convenience Clarity Consistency
6
(c) 2010 University of California, Irvine – André van der Hoek6July 16, 2015 – 13:45:31 A Checklist on Principles and Strategies Principles –keep it simple, stupid! (KISS) –information hiding –acyclic dependencies –… Strategies –program to the interface –refactor –apply software patterns –use aspects –…
7
(c) 2010 University of California, Irvine – André van der Hoek7July 16, 2015 – 13:45:31 A Checklist on Principles and Strategies Principles –keep it simple, stupid! (KISS) –information hiding –acyclic dependencies –… Strategies –program to the interface –refactor –apply software patterns –use aspects –…
8
(c) 2010 University of California, Irvine – André van der Hoek8July 16, 2015 – 13:45:31 Design Patterns “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, Ishikawa, Silverstein 1977] Pattern –name –problem –solution –consequences
9
(c) 2010 University of California, Irvine – André van der Hoek9July 16, 2015 – 13:45:31 Software Design Patterns “Descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context” [Gamma, Helm, Johnson, Vlissides 1995] Pattern –name and classification –intent –also known as –motivation –applicability –structure –participants –collaborations –consequences –implementation –sample code –known uses –related patterns
10
(c) 2010 University of California, Irvine – André van der Hoek10July 16, 2015 – 13:45:31 Patterns Are Designed to Avoid Redesign Creating an object by specifying a class explicitly Dependence on specific operations Dependence on hardware and software platform Dependence on object representations or implementations Algorithmic dependencies Tight coupling Extending functionality by subclassing Inability to alter classes conveniently
11
(c) 2010 University of California, Irvine – André van der Hoek11July 16, 2015 – 13:45:31 Patterns Apply Two Design Principles Program to an interface, not an implementation –interface should be separately defined, using abstract classes Favor object composition over inheritance
12
(c) 2010 University of California, Irvine – André van der Hoek12July 16, 2015 – 13:45:31 Original Catalogue of Patterns Purpose CreationalStructuralBehavioral ScopeClassAbstract MethodAdapter (class)Interpreter Template Method ObjectAbstract Factory Builder Prototype Singleton Adapter (object) Bridge Composite Decorator Façade Flyweight Proxy Chain of Responsibility Command Iterator Mediator Memento Observer State Strategy Visitor
13
(c) 2010 University of California, Irvine – André van der Hoek13July 16, 2015 – 13:45:31 Creational patterns Objectives: –Reduce coupling –Allow for changing implementations –Partitioning the task of creating complex objects How are objects created? Who creates them? What information do they need to create them?
14
(c) 2010 University of California, Irvine – André van der Hoek14July 16, 2015 – 13:45:31 Creational: Singleton Name: Singleton Problem: How can I limit the number of instances created of an object? Solution: Make the constructor private, provide a static method to get an instance Consequences: Hides some dependencies, complicates testing, can lead to performance issues, depends on single VM
15
Singleton What is the code in this class? Where would you use it? © 2007 University of California, Irvine – André van der Hoek15July 16, 2015 – 13:45:31
16
Structural Objectives –Identifying simple ways to realize relationships between entities –Reducing coupling between classes How can I use a method when it has the wrong signature? How can I limit the access to a part of the system? How can I add new functionalities to a system without extending its interface? © 2007 University of California, Irvine – André van der Hoek16July 16, 2015 – 13:45:31
17
(c) 2010 University of California, Irvine – André van der Hoek17July 16, 2015 – 13:45:31 Structural: Adaptor (or wrapper) Name: Adaptor / Adapter / Wrapper Problem: I have a class that calls a method with a specific name (methodA) and I can’t change this class; I have a class that implements the functionality, but the method has a different signature (methodB) Solution: Create a class in the middle that adapts the method invocation from one name to the other Consequences: …
18
Adaptor pattern example How can I display an AddressBook using a JTable? Without coupling AddressBook to JTable © 2007 University of California, Irvine – André van der Hoek18July 16, 2015 – 13:45:31
19
Adaptor pattern example © 2007 University of California, Irvine – André van der Hoek19July 16, 2015 – 13:45:31 Write the code for AddressBookAdaptor
20
(c) 2010 University of California, Irvine – André van der Hoek20July 16, 2015 – 13:45:31 Adaptor (Structural, Object) How are these two different? What impact to the differences have?
21
Behavioral © 2007 University of California, Irvine – André van der Hoek21July 16, 2015 – 13:45:31 Objectives –Provide common communication patterns between objects –Increase flexibility in the communication between objects How can I use a data structure without knowledge of its implementation? How can I implement publish/subscribe functionality in a request/reply P.L. How can I mediate communication to multiple interfaces?
22
Behavioral: Iterator © 2007 University of California, Irvine – André van der Hoek22July 16, 2015 – 13:45:31 Name: Iterator Problem: How can I sequentially iterate over an object aggregation without knowing its implementation Some examples you know? Lets build the UML for an example!
23
Behavioral: Iterator © 2007 University of California, Irvine – André van der Hoek23July 16, 2015 – 13:45:31 Solution: Can you tell me what is the solution? Consequences: What are the consequences? Can you generalize the UML from the example? –classes: Aggregate, Iterator
24
Iterator © 2007 University of California, Irvine – André van der Hoek24July 16, 2015 – 13:45:31 http://www.lepus.org.uk/ref/companion/
25
Design Patterns - part 2 © 2007 University of California, Irvine – André van der Hoek25July 16, 2015 – 13:45:31
26
Behavioral: Observer © 2007 University of California, Irvine – André van der Hoek26July 16, 2015 – 13:45:31 Name: Observer Problem: in OO the only type of interaction that exists is request/reply. What is publish /subscribe? How can we implement it using request / reply?
27
Observer example: Swing © 2007 University of California, Irvine – André van der Hoek27July 16, 2015 – 13:45:31 I want to be notified whenever the user performs an action with the mouse inside a visual component What do I need to do? What does the component class do?
28
(c) 2010 University of California, Irvine – André van der Hoek28July 16, 2015 – 13:45:31 Observer (Behavioral, Object)
29
Observer © 2007 University of California, Irvine – André van der Hoek29July 16, 2015 – 13:45:31 Can you build two sequence diagrams for this pattern: Registering 2 instances of observers for the subject Another object modifies the subject
30
Factory Method + Abstract Factory © 2007 University of California, Irvine – André van der Hoek30July 16, 2015 – 13:45:31 How can I completely isolate concrete classes from clients? Avoiding calling new of a specific concrete class! When would this be useful? Solution: two patterns, one builds on top of the other Create a Factory method that encapsulates this responsibility (Factory Method) Provide an interface both for the products and for the Factory (Abstract Factory)
31
(c) 2010 University of California, Irvine – André van der Hoek31July 16, 2015 – 13:45:31 Factory Method (Creational, Class)
32
(c) 2010 University of California, Irvine – André van der Hoek32July 16, 2015 – 13:45:31 Abstract Factory (Creational, Object)
33
Builder © 2007 University of California, Irvine – André van der Hoek33July 16, 2015 – 13:45:31 Creating a complex object sometimes is a functionality that must be divided: Building each part Consolidating results from the built parts Example Processing a complex XML document and building a data structure from it Processing parts of the XML document (e.g. building Objects) Aggregating these parts into complex data structures (e.g. a tree of objects) Why We can aggregate the parts into different types of structures We can change how each part is built with no impact on the aggregation process
34
(c) 2010 University of California, Irvine – André van der Hoek34July 16, 2015 – 13:45:31 Builder (Creational, Object)
35
Composite © 2007 University of California, Irvine – André van der Hoek35July 16, 2015 – 13:45:31 Problem: In hierarchical data structures processing each element depends on its location in the hierarchy In trees nodes and leafs require different processing The Client that needs to use the data structure should not worry about these problems Solution: Manipulating groups of objects or single instances in a similar way Create a composite object that aggregates other objects and exhibit similar functionality to the contents
36
(c) 2010 University of California, Irvine – André van der Hoek36July 16, 2015 – 13:45:31 Composite (Structural, Object)
37
Decorator © 2007 University of California, Irvine – André van der Hoek37July 16, 2015 – 13:45:31 Problem: How can I add new functionality to a specific instance of an object during execution without modifying other instances? Solution Create an interface (Component) for the services of the ConcreteComponent we want to decorate Create new classes (ConcreteDecorators) for each optional functionality (doStuff) Associate the decorators and the existing components with an abstract Decorator that has an instance of Component
38
(c) 2010 University of California, Irvine – André van der Hoek38July 16, 2015 – 13:45:31 Decorator (Structural, Object)
39
Decorator How would you modify the previous diagram so that a Factory pattern is also included Build a sequence diagram that shows how a ConcreteComponent with and without decorators is created © 2007 University of California, Irvine – André van der Hoek39July 16, 2015 – 13:45:31
40
Proxy How can I make remote and local invocation of a service the same from a the client’s perspective? Solution? –Lets brainstorm! © 2007 University of California, Irvine – André van der Hoek40July 16, 2015 – 13:45:31
41
(c) 2010 University of California, Irvine – André van der Hoek41July 16, 2015 – 13:45:31 Proxy (Structural, Object)
42
Proxy © 2007 University of California, Irvine – André van der Hoek42July 16, 2015 – 13:45:31 What are some consequences of this pattern? What can be some issues related to parameter passing?
43
(c) 2010 University of California, Irvine – André van der Hoek43July 16, 2015 – 13:45:31 More… Patterns –http://en.wikipedia.org/wiki/Software_pattern –http://c2.com/ppr/ –http://www.lepus.org.uk/ref/companion/ Interaction design patterns –http://en.wikipedia.org/wiki/Interaction_design_pattern Anti-Patterns –http://en.wikipedia.org/wiki/Anti-pattern#Programming_anti-patterns Refactoring –http://en.wikipedia.org/wiki/Refactoring And numerous, numerous, numerous books
44
© 2010 University of California, Irvine – André van der Hoek44February 8, 2010 – 21:49:30 Assignment 4 – Design Patterns Identify existing patterns in Mylyn Identify at 4-6 existing patterns (depending on group size) Improve the design of Mylyn by replacing existing structures in the UML model with appropriate patterns –start with any of the designs available to your group You should apply 4-6 different design patterns –at least two not discussed in class Each use of a pattern should be carefully motivated in a brief accompanying document
45
© 2010 University of California, Irvine – André van der Hoek45February 8, 2010 – 21:49:30 Assignment 4 – Design Patterns The patterns must be part of the 23 defined in the book! the identification of patterns you cannot use the following : Singleton, Factory Method, Iterator, Proxy, Façade For the second task (improving the design) you cannot use the following: Singleton, Factory Method, Iterator, Proxy, Façade, Prototype
46
© 2010 University of California, Irvine – André van der Hoek46February 8, 2010 – 21:49:30 Assignment 4 – Design Patterns Each group must turn in: –the original UML diagram from which you started –a new UML diagram, with each pattern precisely highlighted as to where it resides in the UML diagram –a document describing the motivation for each pattern the impact the application of the pattern has on the original design (i.e., how far reaching is the change to incorporate the pattern?) –graded on usefulness of the pattern, diversity of patterns, rationale, and level of design understanding Each person also needs to submit a team evaluation (new forms available on class webpage) Printed copy due Wednesday, February 16 th at the beginning of class
47
© 2010 University of California, Irvine – André van der Hoek47February 8, 2010 – 21:49:30 Further Tips Read the book Discuss the patterns with each other Imagine possible changes against which you would like to insulate Try different parts of the code Use the UML
48
(c) 2010 University of California, Irvine – André van der Hoek48July 16, 2015 – 13:45:31 Command (Behavioral, Object)
49
(c) 2010 University of California, Irvine – André van der Hoek49July 16, 2015 – 13:45:31 State (Behavioral, Object)
50
(c) 2010 University of California, Irvine – André van der Hoek50July 16, 2015 – 13:45:31 Visitor (Behavioral, Object)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.