Design Patterns Lecture 2

Similar documents
Software Eningeering. Lecture 9 Design Patterns 2

Object-Oriented Oriented Programming

CSCI 253. Overview. The Elements of a Design Pattern. George Blankenship 1. Object Oriented Design: Iterator Pattern George Blankenship

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

SDC Design patterns GoF

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

COSC 3351 Software Design. Design Patterns Structural Patterns (I)

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

6.3 Patterns. Definition: Design Patterns

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

The Strategy Pattern Design Principle: Design Principle: Design Principle:

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Brief Note on Design Pattern

» Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request!

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

Design Patterns: Structural and Behavioural

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

Design Patterns Reid Holmes

COSC 3351 Software Design. Design Patterns Behavioral Patterns (I)

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Design patterns. Jef De Smedt Beta VZW

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

Introduction to Software Engineering: Object Design I Reuse & Patterns

2.1 Design Patterns and Architecture (continued)

Design Patterns. Comp2110 Software Design. Department of Computer Science Australian National University. Second Semester

2.1 Design Patterns and Architecture (continued)

Last Lecture. Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/ Spring Semester, 2005

Topics in Object-Oriented Design Patterns

Design Patterns V Structural Design Patterns, 2

CISC 322 Software Architecture

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

A Reconnaissance on Design Patterns

CS 2720 Practical Software Development University of Lethbridge. Design Patterns

Software Design COSC 4353/6353 D R. R A J S I N G H

Information systems modelling UML and service description languages

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

Design Patterns. An introduction

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

Object-oriented Software Design Patterns

Slide 1. Design Patterns. Prof. Mirco Tribastone, Ph.D

DESIGN PATTERN - INTERVIEW QUESTIONS

DESIGN PATTERNS Course 4

Lecture 20: Design Patterns II

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

An Introduction to Patterns

Using Design Patterns in Java Application Development

Design Patterns. SE3A04 Tutorial. Jason Jaskolka

Design of Software Systems (Ontwerp van SoftwareSystemen) Design Patterns Reference. Roel Wuyts

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

UNIT I Introduction to Design Patterns

SYLLABUS CHAPTER - 1 [SOFTWARE REUSE SUCCESS FACTORS] Reuse Driven Software Engineering is a Business

SOFTWARE PATTERNS. Joseph Bonello

Appendix-A. A.1 Catalogues of Design Patterns. Below is the definition for each design pattern using the FINDER notation, followed

Object Oriented. Analysis and Design

Laboratorio di Sistemi Software Design Patterns 2

Design Patterns Revisited

TDDB84: Lecture 6. Adapter, Bridge, Observer, Chain of Responsibility, Memento, Command. fredag 4 oktober 13

The Chain of Responsibility Pattern

The Chain of Responsibility Pattern. Design Patterns In Java Bob Tarr

Object Oriented Paradigm

Dr. Xiaolin Hu. Review of last class

COURSE 4 DESIGN PATTERNS

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

Applying Design Patterns to SCA Implementations

TDDB84. Lecture 2. fredag 6 september 13

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

Tuesday, October 4. Announcements

Design Patterns #3. Reid Holmes. Material and some slide content from: - GoF Design Patterns Book - Head First Design Patterns

Egon Borger (Pisa) Capturing Design Pattern Abstractions by ASMs

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

The Design Patterns Matrix From Analysis to Implementation

Design Patterns. Definition of a Design Pattern

UNIT I Introduction to Design Patterns

Design Patterns. (and anti-patterns)

A few important patterns and their connections

Plan. A few important patterns and their connections. Singleton. Singleton: class diagram. Singleton Factory method Facade

Design Patterns Reid Holmes

Details of Class Definition

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

EECS 4314 Advanced Software Engineering. Topic 04: Design Pattern Review Zhen Ming (Jack) Jiang

Design Patterns. Dr. Rania Khairy. Software Engineering and Development Tool

CSCI Object Oriented Design: Frameworks and Design Patterns George Blankenship. Frameworks and Design George Blankenship 1

Introduction and History

Design patterns. OOD Lecture 6

3 Product Management Anti-Patterns by Thomas Schranz

Last Lecture. Lecture 26: Design Patterns (part 2) State. Goals of Lecture. Design Patterns

OODP Session 4. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

Design Patterns IV Structural Design Patterns, 1

Composite Pattern. IV.4 Structural Pattern

Object-Oriented Design

1 Software Architecture

Chain of Responsibility

Lecture 17: Patterns Potpourri. Copyright W. Howden 1

Applying the Observer Design Pattern

Design for change. You should avoid

UNIT-I. Introduction, Architectural Styles, Shared Information Systems

Design Patterns IV. Alexei Khorev. 1 Structural Patterns. Structural Patterns. 2 Adapter Design Patterns IV. Alexei Khorev. Structural Patterns

Design Pattern and Software Architecture: IV. Design Pattern

Design Patterns. "Gang of Four"* Design Patterns. "Gang of Four" Design Patterns. Design Pattern. CS 247: Software Engineering Principles

Transcription:

Design Patterns Lecture 2 Josef Hallberg josef.hallberg@ltu.se 1 Patterns covered Creational Abstract Factory, Builder, Factory Method, Prototype, Singleton Structural Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy Behavioral Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method, Visitor 2 1

Structural Patterns How classes and objects are composed to form a larger structure Structural Class patterns Uses inheritance to compose interfaces or implementations Structural Object patterns describes ways to compose objects to realize new functionality Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy 3 Adapter Problem/Applicability Interface mismatch Create a reusable class that cooperates with unrelated or unforeseen classes 4 2

Adapter Solution Class Adapter Client Target + Request() Adaptee + SpecificRequest() SpecificRequest(); Adapter + Request() 5 Adapter Solution Object Adapter Client Target +Request() Adaptee + SpecificRequest() adaptee->specificrequest(); Adapter +Request() (adaptee) 6 3

Adapter -- example Bought a new positioning system Code included but interfaces mismatch Make an adapter to include the new positioning system into our platform 7 Adapter Example public interface PullPositionDevice { public Coordinate getposition(); } 8 4

Adapter Example public interface PullPositionDevice { public Coordinate getposition(); } public class YPositionDevice extends YPosition implements PullPositionDevice { } public Coordinate getposition() { String position = super.positionrequest(); Coordinate c = //transform String to Coordinate return c; } 9 Adapter Example public interface PullPositionDevice { public Coordinate getposition(); } public class YPositionDevice implements PullPositionDevice { private YPosition yposition; } public Coordinate getposition() { String position = yposition.positionrequest(); Coordinate c = // transform String to Coordinate return c; } 10 5

Class adapter Consequences Adapts Adoptee to Target by committing to a concrete adapter class Lets Adapter override some of Adaptee s behavior Can still act as the original Type if necessary Object Adapter Lets a single Adapter work with many Adaptees Makes it harder to override Adaptee behavior Can not act as the original Type 11 Bridge -- Problem Class with some parts consisting of general code some parts have several different implementations Could use inheritance put the general code in each subclass might not be flexible enough 12 6

Bridge Solution Decouple the code General code Several different implementations The Bridge pattern decouples an abstraction from its implementation so that the two can vary independently 13 Bridge -- UML Abstraction + Operation() Implementor + OperationImp() Imp->OperationImp() RefinedAbstraction ConcreteImlementorA + OperationImp() ConcreteImlementorB + OperationImp() 14 7

Bridge example Application to run on a desktop computer as well as a mobile phone Screen size varies Input capabilities vary Instead of making two different applications we can make a bridge to the device specific implementations 15 Bridge example cont. A1. public class Abstraction { A2. A3. public void close(){ A4. System.exit(0); A5. } A6. public void open() { A7. // do cool open stuff A8. } A9. public void undo() { A10. // undo stuff A11. } A12. A13. public void operationx() { A14. imp = getimp(); A15. imp->operationx(); A16. } A17. } C1. public interface Implementor { C2. public void operationximpl(); C3. } D1. public class ConcreteImplA D2. implements Implementor D3. { D4. public void operationximpl() D5. { D6. // Do things D7. } D8. } E1. public class ConcreteImplB E2. implements Implementor E3. { E4. public void operationximpl() E5. { E6. // Do other things E7. } E8. } 16 8

Bridge -- usage Consequences Decoupling interface and implementation Improved extensibility Hiding implementation details from clients 17 Composite -- Problem Structured design composed of several graphical objects Could implement in a single class Larger designs will be hard to read it will be less flexible 18 9

Composite Solution Compose objects into a tree structure with composites and leaves acomposite for all children execute operation aleaf aleaf acomposite aleaf aleaf aleaf aleaf 19 Composite Solution cont. In Java AWT there is a similar pattern for graphic objects. java.awt.container java.awt.component Component Container Button TextArea Label Window Panel Frame Dialog 20 10

Composite -- UML Client Component + Operation() + Add(Component) + Remove(Component) + GetChild(int) : Component Leaf + Operation() Component + Operation() + Add(Component) + Remove(Component) + GetChild(int) : Component children forall g in children g.operation(); 21 Composite -- example Map application map as background A dot (your position) A scale marker Scale marker could be a composite in itself (line and text) When the map is redrawn it also lets all its children redraw 22 11

Composite example cont. A1. public abstract class MapObject { A2. protected boolean visible; A3. public void setvisible(boolean see) { A4. this.visible = see; A5. } A6. public abstract void paint(graphics g); A7. } B1. public class MapCompass extends MapObject { B2. public void paint(graphics g) { B3. if (g!=null) { B4. if(visible) { B5. // paint the compass B6. } } } } C1. public class MapPosition extends MapObject { C2. public void paint(graphics g) { C3. if (g!=null) { C4. if(visible) { C5. // paint the dot showing the position C6. } } } } 23 Composite example cont.2. 1. public class MapCanvas extends Canvas implements MapObject, Component 2. { 3. //... 4. //... 5. public void addmapobject(mapobject mo) { 6. children.addelement(mo); 7. } 8. 9. public void paint(graphics g) { 10. // paint itself 11. for(int i=0; i<children.size(); i++) { 12. ((MapObject)children.elementAt(i)).paint(aGraphicsBuffer); 13. } 14. } 15. 16. //... 17. //... 18.} 24 12

Composite -- usage Consequences Defines class hierarchies consisting of primitive objects and composite objects Makes the client simple Makes it easier to add new kinds of components 25 Let s take a break! =) 26 13

Façade Problem/Applicability You want to provide a uniform interface to a set of interfaces in a subsystem You want to make a subsystem easier to use There are many dependencies between clients and subsystems 27 Façade Problem/Applicability Client1 Client2 Client3 Client4 subsystem classes 28 14

Façade Solution Client1 Client2 Client3 Client4 subsystem classes Facade 29 Facade Example A1. public class Client1 { A2. FacadeClass1 c1; A3 FacadeClass2 c2; A4.... A5. A6. public void operation() { A7. c1.operation1(); A8. c2.operation2(); A9.... A10. } A11. } B1. public class Client1 { B2. Facade facade; B3. B4. public void operation() { B5. facade.facadeop1(); B6. facade.facadeop2(); B7... B8. } OR C1. public void operation() { C2. facade.facadeop(); C3. } C4. } 30 15

Façade Consequences Shields clients from subsystem components Promotes weak coupling between subsystem and client Doesn t prevent use of subsystem classes if needed 31 Summary - structural Adapter Lets classes with incompatible interfaces work together Bridge Decouple an abstraction from its implementation so that the two can vary independently Composite Lets clients treat individual objects and compositions of objects uniformly Facade Provide a unified interface to a set of interfaces in a subsystem 32 16

Behavioural patterns Concerned with algorithms and assignment of responsibilities. Complex control-flow that s hard to follow at run-time class: use inheritance to distribute behaviour between classes object: object composition to, for example, describe how a group of objects should cooperate 33 Chain of Responsibility Problem You want to avoid coupling the sender of a request with a certain receiver More than one object may handle a request and the handler isn t known beforehand The set of objects that can handle a request should be specified dynamically 34 17

Chain of Responsibility Solution Client Handler successor +handlerequest() : void ConcreteHandler1 +handlerequest(): void ConcreteHandler2 +handlerequest(): void 35 Chain of Responsibility Solution Client HelpHandler +handlehelp() : void handler handler->handlehelp() Application +handlehelp(): void Dialog Widget +handlehelp(): void Button +handlehelp(): void -showhelp(): void if can handle { showhelp(); } else { Handler::handleHelp(); } 36 18

Chain of Responsibility Consequences Reduced coupling Added flexibility in assigning responsibilities to objects Receipt isn t guaranteed 37 Iterator Problem/Applicability You wish to create an aggregated object Access the elements sequentially Don t want to expose underlying representation Want to support multiple types of traversals Remember state 38 19

Iterator Solution Aggregate + createiterator() Iterator + first() + next() + isdone() + currentitem() ConcreteAggregate + createiterator() return new ConcreteIterator(this) ConcreteIterator + first() + next() + isdone() + currentitem() 39 Iterator -- example 40 20

Iterator Example 1. public class Client { 2. private Aggregate agg; 3. 4. public void setaggregate(aggregate agg) { 5. this.agg = agg; 6. } 7. 8. public void print() { 9. Iterator iterator = this.agg.createiterator(); 10. while (!iterator.isdone()) { 11. System.out.println(iterator.currentItem()); 12. iterator.next(); 13. } 14.} 15.} 41 Iterator Consequences It supports variations in the traversal of an aggregate Iterators simplify the Aggregate interface More than one traversal can be pending on an aggregate Remembers its own state 42 21

Iterator Implementation Issues Who controls the iteration? Who defines the traversal algorithm? How robust is the iterator? Additional Iterator operations 43 Summary - behavioral Chain of Responsibility Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Iterator Provide a way to access the elements of an aggregate object sequentially 44 22

Questions? 45 23