Composite Pattern. IV.4 Structural Pattern

Similar documents
Design Pattern and Software Architecture: IV. Design Pattern

SDC Design patterns GoF

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

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

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

Object-Oriented Oriented Programming

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

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

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

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

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

Introduction to Software Engineering: Object Design I Reuse & Patterns

Design Patterns Reid Holmes

Using Design Patterns in Java Application Development

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

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

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

Topics in Object-Oriented Design Patterns

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

Design patterns. Jef De Smedt Beta VZW

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

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

Design Pattern: Composite

Design Patterns. Decorator. Oliver Haase

Design Patterns. An introduction

Design Patterns: Structural and Behavioural

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

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

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

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

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

UNIT I Introduction to Design Patterns

UNIT I Introduction to Design Patterns

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

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

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

2.1 Design Patterns and Architecture (continued)

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

2.1 Design Patterns and Architecture (continued)

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

Object-oriented Software Design Patterns

INSTITUTE OF AERONAUTICAL ENGINEERING

Object Oriented Paradigm

Object-Oriented Design

Information systems modelling UML and service description languages

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

Design Patterns IV Structural Design Patterns, 1

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

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

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

Design Patterns. (and anti-patterns)

Brief Note on Design Pattern

Design Patterns. SE3A04 Tutorial. Jason Jaskolka

An Introduction to Patterns

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

A Reconnaissance on Design Patterns

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

Design Patterns V Structural Design Patterns, 2

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

Design Patterns Reid Holmes

Applying Design Patterns to accelerate development of reusable, configurable and portable UVCs. Accellera Systems Initiative 1

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

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

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

Chapter 5 Object-Oriented Programming

Chapter 1. OO e OO. (Pattern) Alexander) (context) (elements) k. (issue) Š. (name) (classification)

What is a Pattern? Lecture 40: Design Patterns. Elements of Design Patterns. What are design patterns?

LECTURE NOTES ON DESIGN PATTERNS MCA III YEAR, V SEMESTER (JNTUA-R09)

DESIGN PATTERNS SURESH BABU M ASST PROFESSOR VJIT

design patterns FOR B.tech (jntu - hyderabad & kakinada) (IV/I - CSE AND IV/II - IT) CONTENTS 1.1 INTRODUCTION TO DESIGN PATTERNS TTERNS... TTERN?...

CS342: Software Design. November 21, 2017

DESIGN PATTERN - INTERVIEW QUESTIONS

Design patterns. OOD Lecture 6

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

SOFTWARE PATTERNS. Joseph Bonello

Design patterns. Valentina Presutti courtesy of Paolo Ciancarini

Applying the Observer Design Pattern

Design Patterns. Observations. Electrical Engineering Patterns. Mechanical Engineering Patterns

Design for change. You should avoid

Lecture 17: Patterns Potpourri. Copyright W. Howden 1

Tuesday, October 4. Announcements

Design Patterns. Gunnar Gotshalks A4-1

Software Eningeering. Lecture 9 Design Patterns 2

C++ for System Developers with Design Pattern

Design Patterns Lecture 2

Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns

EMBEDDED SYSTEMS PROGRAMMING Design Patterns

administrivia today UML start design patterns Tuesday, September 28, 2010

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

Applying the Decorator Design Pattern

Design patterns generic models

COSC 3351 Software Design. Design Patterns Structural Patterns (II) Edgar Gabriel. Spring Decorator

Design Patterns. Lecture 10: OOP, autumn 2003

What are patterns? Design Patterns. Design patterns. Creational patterns. The factory pattern. Factory pattern structure. Lecture 10: OOP, autumn 2003

Design Patterns. CSE870: Advanced Software Engineering (Design Patterns): Cheng

Pro Objective-C Design Patterns for ios

Overview CS Kinds of Patterns. Design Pattern. Factory Pattern Rationale. Kinds of Factory Patterns

Applying Design Patterns to SCA Implementations

Design Patterns! Acknowledgements!

Design Pattern and Software Architecture: IV. Design Pattern

Transcription:

IV.4 Structural Pattern Motivation: Compose objects to realize new functionality Flexible structures that can be changed at run-time Problems: Fixed class for every composition is required at compile-time otherwise Patterns: Composite Pattern Decorator Pattern Bridge Pattern Adapter Pattern Facade Pattern Flyweight Pattern [Gamma+1994] IV-25 Composite Pattern Intent: Compose objects into a tree to represent part-whole hierarchies where the composition and its parts are treated uniformly Related Patterns Often the Decorator Pattern is used combined with the Composite Pattern. The Iterator Pattern can be used to traverse composite structures. The Visitor pattern can be used to localize behaviour that would otherwise be distributed across the composites and leafs. IV-26 1

Composite Pattern Example Support a tree (DAG) of similar/uniform treated objects (Example: Lexi document structure) Advantages: Uniform simply extendable structure Implicit operation forwarding Simple recursive traversing Naïve OO solution: Many classes Many casts IV-27 Composite Pattern Realization IV-28 2

Composite Pattern Realization Idea: Common abstract superclass (Glyph) determines ALL essential properties For application this rather abstract superclass is often enough Composite forwards his operations to all children => Apply operations uniformly on (sub-)trees Implementation: Explicit parent reference? => Better use association instead of simple reference Sharing components? Later => Design Pattern Flyweigth Maximizing base-class interface? Yes! - subclasses may inherit useless methods + more uniform classes prevent casts Where are the child managing operations Add and Remove declared? Composite => type safe, but contradicts "maximizing base-class interface" Component => not type safe because leafs will never contain other composites Child ordering? Often yes! Who should delete components? Composition or Shared Aggregation? Often Composition! IV-29 Implementation Variant 1 Efficient Variant: Distinguish char from the rest Clean Variant Keep uniform treatment IV-30 3

$ Decorator Pattern Intent: Attach additional functionality to objects and not classes dynamically Alternative to subclassing! Also Known As: wrapper Related Pattern: Adapter change interface degenerated form of Composite that adds responsibilities change skin rather than internals such as Strategy IV-31 Decorator Pattern Example Example:! Add Frames, Scroll Bars, Borders, Background, Shadows, etc. to the document root Basic OO: " Redefine method Problem: # Not flexible Add/Remove "Decorators Leads to complex case statements IV-32 4

& ) Decorator Pattern Realization Idea: % Use explicit Decoration objects Dynamic changes possible Design Goal: ' "seamless" decoration => Standard: ( Forward most operations Decorate some operations and add functionality and status where required IV-33 Decorator Pattern Realization IV-34 5

* +, - / 0 1 2 Bridge Pattern Intent: Decouple abstraction and implementation of class hierarchies Also Known As: Handle/Body Motivation: Hierarchy of classes with similar functionality Achieve platform independence using abstract superclasses and platform specific subclasses Explosion of the class hierarchy Related Patterns: An Abstract Factory may create and configure a Bridge While a Bridge is designed up-front an Adapter might be used in already existing systems IV-35 Bridge Pattern Example Design Aspects:. "double dispatch : the kind of window and its specific implementation determines the finally called operation Flexible separation of interface objects and implementation objects Implementation can be exchanged without recompilation => shared libraries WindowImp interface is sufficient for all specific kind of windows WindowImp subclasses often adapter for the related basic libraries IV-36 6

5 6 Bridge Pattern Realization IV-37 Adapter Pattern Intent: Adapt extern interfaces (libraries, modules,...) to the required internal interfaces Also Known As: Wrapper Example: 3 Type conversions for parameters Related Pattern: 4 The Bridge is used to separate the interface form its implementation while the adapter is used to change the interface to an existing object Proxies are representatives for other objects, but do not change the interface Decorator keeps the interface and adjusts functionality while the Adapter adjust the interfacse and keeps the functionality IV-38 7

9 : ; Adapter Pattern Realization IV-39 Facade Pattern Intent: Provide a unified interface to a set of interfaces in a subsystem Motivation: 7 Support subsystem encapsulation using a single explicit interface object Related Patterns: 8 An Abstract Factory may provide an interface for creating objects in a subsystem independent way An Abstract Factory can be used as alternative by hiding subsystem specific classes A Mediator has similar functionality, but has two combined systems and does not abstract form one of them Often Facade objects are also Singletons IV-40 8

> B Flyweight Pattern Intent: Use object sharing to support huge numbers of fine-grained objects efficiently Motivation: < Benefit from using objects also where their unshared usage would be prohibitively expensive Related Patterns: = Often combined with Composite State and Strategy objects are often Flyweights IV-41 Flyweight Pattern Example Problem: Number of character objects becomes a problem when for each character font attributes etc. have to be stored composite (row) composite (column) composite (row) Intrinsic state:? Independent of context Extrinsic state: @ Varies with the context Example: A Character (intrinsic) Font, location (extrinsic) composite (row) a p p a r e n t composite (column) composite (row) a p p a r e n t a e n p r t z IV-42 9

M IV.5 Behavioural Pattern Motivation: C Assign responsibilities between objects to realize algorithms and behaviour Problem: D How to distribute behaviour between different classes [Gamma+1994] Patterns: E Strategy Pattern F Template Method G Iterator Pattern H Visitor Pattern I Command Pattern J Memento Pattern K Hook Pattern IV-43 Strategy Pattern Intent: Encapsulation of a family of dynamically interchangeable algorithms Also Known As: Policy Problem: L Best" algorithm can often not uniquely determined due to trade-offs: Run-time <-> Memory, Run-time <-> Quality Best solution depends on problem characteristics: e.g. for sorting: data structure, stability, problem size,... Related Pattern: N Strategy objects often make good flyweights [Gamma+1994] IV-44 10

P Q Strategy Pattern Example Formatter: O quick&dirty slow&fine (TeX) IV-45 Strategy Pattern Realization Idea: R explicit algorithm object S Abstract algorithm superclass T "Delegation" U dynamically assign what algorithm should be used IV-46 11

W Y Z Strategy Pattern Realization Key Issue: Strategy / Context Interface Strategy knows" Context structure: V Strategy has reference of Context/Document Context provides fat" interface => tight Context/Strategy coupling Context propagation via parameter: X Context provides ALL required information to the Strategy: loosely coupling Problem: TEXFormatter requires more details than Simpleformatter! => always provide MAXIMAL information []\_^a`cbedgfihkj lnmpo o o ^q`rbpjso o o oilcm t t uv`cw w \xuihnuzyzfvba{ `v^~}sfxbafvcbafz}ryg g} r\ihkƒz`cbadgfih hq\zb j lko r`c[]\_^q`rbedgfihkj }vl t tc ˆ r v g}gfvba\ fv _ ašs{_hq\v s`vœ o o o See also: Visitor Design Pattern IV-47 Strategy Pattern Realization Pros: Ž Families of algorithms An alternative to subclassing Strategies eliminate conditional (switch) statements (dynamic) choice of implementation Cons: Delegation => communication overhead IV-48 12

Template Method Pattern Intent: Define a skeleton algorithm with steps deferred to subclasses Example: Sort algorithm parameterised with iterator, less-op, swapop Related Pattern: Factory Methods are often called by Template Methods Template Methods use inheritance to vary parts of the algorithm while Strategy use delegation to vary the entire algorithm [Gamma+1994] IV-49 Template Method Example IV-50 13

œ Template Method Realization Design aspects: An OO classics! Reuse superclass with extensive implementation Simple application/context specific subclasses define the details in deferred operations Extract common code/functionality into superclass Implementation: š Access control: deferred operations have to be protected Minimizing the number of required deferred operations Naming Convention: name deferred operations doxxx IV-51 14