+ All Categories
Home > Documents > (c) 2010 University of California, Irvine – André van der Hoek1July 16, 2015 – 13:45:31...

(c) 2010 University of California, Irvine – André van der Hoek1July 16, 2015 – 13:45:31...

Date post: 22-Dec-2015
Category:
View: 212 times
Download: 0 times
Share this document with a friend
Popular Tags:
50
(c) 2010 University of California, Irvine – André van der Hoek 1 March 21, 2022 – 19:26:28 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.
Transcript

(c) 2010 University of California, Irvine – André van der Hoek 1April 19, 2023 – 01:07:30

Informatics 122Software Design II

Informatics 122Software Design II

Lecture 8Nick Lopez

Duplication of course material for any commercial purpose without the explicit written permission of the professor is prohibited.

(c) 2010 University of California, Irvine – André van der Hoek 2April 19, 2023 – 01:07:30

Today’s LectureToday’s Lecture

Design patterns

Assignment 4

(c) 2010 University of California, Irvine – André van der Hoek 3April 19, 2023 – 01:07:30

Fundamental PrinciplesFundamental Principles

Apply rigor Separate concerns

– modularize– abstract

Anticipate change Generalize Work incrementally

(c) 2010 University of California, Irvine – André van der Hoek 4April 19, 2023 – 01:07:30

A Checklist on Overall DesignA 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)

(c) 2010 University of California, Irvine – André van der Hoek 5April 19, 2023 – 01:07:30

A Checklist on Class DesignA Checklist on Class Design

Cohesion Completeness Convenience Clarity Consistency

(c) 2010 University of California, Irvine – André van der Hoek 6April 19, 2023 – 01:07:30

A Checklist on Principles and StrategiesA 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– …

(c) 2010 University of California, Irvine – André van der Hoek 7April 19, 2023 – 01:07:30

A Checklist on Principles and StrategiesA 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– …

(c) 2010 University of California, Irvine – André van der Hoek 8April 19, 2023 – 01:07:30

Design PatternsDesign 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

(c) 2010 University of California, Irvine – André van der Hoek 9April 19, 2023 – 01:07:30

Software Design PatternsSoftware 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

(c) 2010 University of California, Irvine – André van der Hoek 10April 19, 2023 – 01:07:30

Patterns Are Designed to Avoid RedesignPatterns 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

(c) 2010 University of California, Irvine – André van der Hoek 11April 19, 2023 – 01:07:30

Patterns Apply Two Design PrinciplesPatterns Apply Two Design Principles

Program to an interface, not an implementation– interface should be separately defined, using abstract

classes Favor object composition over inheritance

(c) 2010 University of California, Irvine – André van der Hoek 12April 19, 2023 – 01:07:30

Original Catalogue of PatternsOriginal Catalogue of Patterns

Purpose

Creational Structural Behavioral

Scope

Class Abstract Method Adapter (class) InterpreterTemplate Method

Object

Abstract FactoryBuilderPrototypeSingleton

Adapter (object)BridgeCompositeDecoratorFaçadeFlyweightProxy

Chain of ResponsibilityCommandIteratorMediatorMementoObserverStateStrategyVisitor

(c) 2010 University of California, Irvine – André van der Hoek 13April 19, 2023 – 01:07:30

Creational patternsCreational 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?

(c) 2010 University of California, Irvine – André van der Hoek 14April 19, 2023 – 01:07:30

Creational: SingletonCreational: 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

SingletonSingleton

What is the code in this class?

Where would you use it?

© 2007 University of California, Irvine – André van der Hoek 15April 19, 2023 – 01:07:30

StructuralStructural

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 Hoek 16April 19, 2023 – 01:07:30

(c) 2010 University of California, Irvine – André van der Hoek 17April 19, 2023 – 01:07:30

Structural: Adaptor (or wrapper)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: …

Adaptor pattern exampleAdaptor pattern example

How can I display an AddressBook using a JTable? Without coupling AddressBook to JTable

© 2007 University of California, Irvine – André van der Hoek 18April 19, 2023 – 01:07:30

Adaptor pattern exampleAdaptor pattern example

© 2007 University of California, Irvine – André van der Hoek 19April 19, 2023 – 01:07:30

Write the code for AddressBookAdaptor

(c) 2010 University of California, Irvine – André van der Hoek 20April 19, 2023 – 01:07:30

Adaptor (Structural, Object)Adaptor (Structural, Object)

• How are these two different?• What impact to the differences have?

BehavioralBehavioral

© 2007 University of California, Irvine – André van der Hoek 21April 19, 2023 – 01:07:30

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?

Behavioral: IteratorBehavioral: Iterator

© 2007 University of California, Irvine – André van der Hoek 22April 19, 2023 – 01:07:30

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!

Behavioral: IteratorBehavioral: Iterator

© 2007 University of California, Irvine – André van der Hoek 23April 19, 2023 – 01:07:30

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

IteratorIterator

© 2007 University of California, Irvine – André van der Hoek 24April 19, 2023 – 01:07:30

http://www.lepus.org.uk/ref/companion/

Design Patterns - part 2Design Patterns - part 2

© 2007 University of California, Irvine – André van der Hoek 25April 19, 2023 – 01:07:30

Behavioral: ObserverBehavioral: Observer

© 2007 University of California, Irvine – André van der Hoek 26April 19, 2023 – 01:07:30

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?

Observer example: SwingObserver example: Swing

© 2007 University of California, Irvine – André van der Hoek 27April 19, 2023 – 01:07:30

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?

(c) 2010 University of California, Irvine – André van der Hoek 28April 19, 2023 – 01:07:30

Observer (Behavioral, Object)Observer (Behavioral, Object)

ObserverObserver

© 2007 University of California, Irvine – André van der Hoek 29April 19, 2023 – 01:07:30

Can you build two sequence diagrams for this pattern: Registering 2 instances of observers for the subject Another object modifies the subject

Factory Method + Abstract FactoryFactory Method + Abstract Factory

© 2007 University of California, Irvine – André van der Hoek 30April 19, 2023 – 01:07:30

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)

(c) 2010 University of California, Irvine – André van der Hoek 31April 19, 2023 – 01:07:31

Factory Method (Creational, Class)Factory Method (Creational, Class)

(c) 2010 University of California, Irvine – André van der Hoek 32April 19, 2023 – 01:07:31

Abstract Factory (Creational, Object)Abstract Factory (Creational, Object)

BuilderBuilder

© 2007 University of California, Irvine – André van der Hoek 33April 19, 2023 – 01:07: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

(c) 2010 University of California, Irvine – André van der Hoek 34April 19, 2023 – 01:07:31

Builder (Creational, Object)Builder (Creational, Object)

CompositeComposite

© 2007 University of California, Irvine – André van der Hoek 35April 19, 2023 – 01:07: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

(c) 2010 University of California, Irvine – André van der Hoek 36April 19, 2023 – 01:07:31

Composite (Structural, Object)Composite (Structural, Object)

DecoratorDecorator

© 2007 University of California, Irvine – André van der Hoek 37April 19, 2023 – 01:07: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

(c) 2010 University of California, Irvine – André van der Hoek 38April 19, 2023 – 01:07:31

Decorator (Structural, Object)Decorator (Structural, Object)

DecoratorDecorator

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 Hoek 39April 19, 2023 – 01:07:31

ProxyProxy

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 Hoek 40April 19, 2023 – 01:07:31

(c) 2010 University of California, Irvine – André van der Hoek 41April 19, 2023 – 01:07:31

Proxy (Structural, Object)Proxy (Structural, Object)

ProxyProxy

© 2007 University of California, Irvine – André van der Hoek 42April 19, 2023 – 01:07:31

What are some consequences of this pattern?

What can be some issues related to parameter passing?

(c) 2010 University of California, Irvine – André van der Hoek 43April 19, 2023 – 01:07:31

More…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

© 2010 University of California, Irvine – André van der Hoek 44February 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

© 2010 University of California, Irvine – André van der Hoek 45February 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

© 2010 University of California, Irvine – André van der Hoek 46February 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 16th at the beginning of class

© 2010 University of California, Irvine – André van der Hoek 47February 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

(c) 2010 University of California, Irvine – André van der Hoek 48April 19, 2023 – 01:07:31

Command (Behavioral, Object)Command (Behavioral, Object)

(c) 2010 University of California, Irvine – André van der Hoek 49April 19, 2023 – 01:07:31

State (Behavioral, Object)State (Behavioral, Object)

(c) 2010 University of California, Irvine – André van der Hoek 50April 19, 2023 – 01:07:31

Visitor (Behavioral, Object)Visitor (Behavioral, Object)


Recommended