2.1 design patterns and architecture (continued) · n the 23 gang-of-four (gof) design patterns...

45
© Fachgebiet Softwaretechnik, Heinz Nixdorf Institut, Universität Paderborn 2.1 Design Patterns and Architecture (continued) 1. Introduction 2. Model Construction 2.1 Design Patterns and Architecture 2.2 State Machines 2.3 Abstract State Machines 2.4 Timed Automata and RTSC 2.5 Graph Grammars 3. Code Generation 4. Testing 5. Model Checking MBSE - 2.1 Design Patterns and Architecture 1

Upload: hoangnga

Post on 05-Jun-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

2.1 Design Patterns and

Architecture (continued)

1. Introduction

2. Model Construction

2.1 Design Patterns and

Architecture

2.2 State Machines

2.3 Abstract State Machines

2.4 Timed Automata and RTSC

2.5 Graph Grammars

3. Code Generation

4. Testing

5. Model Checking

MBSE - 2.1 Design Patterns and Architecture 1

Page 2: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Reminder:

What are Design Patterns?

Approved solutions to recurring software engineering tasks

Reusable design ideas (class structures, modularization,

collaboration,…)

Patterns have an intent (e.g. making an algorithm easily

exchangeable and simplify future changes) as well as benefits

and drawbacks (some future changes become easy, others may

become more difficult)

Correctly applied, patterns increase software

quality by increasing flexibility and reducing

maintenance effort

Most famous design patterns:[Gamma et al., Design Patterns – Elements of Reusable Object-oriented Software, 1995]

MBSE - 2.1 Design Patterns and Architecture 2

Page 3: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

The 23 Gang-of-Four (GoF)

Design Patterns

Gang-of-Four = authors of the design patterns book:

Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides

In this lecture, we will talk about the highlighted patterns:

Creational

Class Class Adapter Interpreter

Template Method

Object Abstract Factory

Builder

Prototype

Singleton

Object Adapter

Bridge

Composite

Decorator

Facade

Flyweight

Proxy

Chain of Responsibility

Command

Iterator

Mediator

Memento

Observer

State

Strategy

Visitor

Scope

Purpose

Structural Behavioral

Factory Method

MBSE - 2.1 Design Patterns and Architecture 3

Page 4: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Editor preview:

Example: Developing a Class

Diagram Editor

MBSE - 2.1 Design Patterns and Architecture 4

Page 5: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Eclipse

Open source IDE framework (Integrated Development

Environment) originally developed by IBM in 2001

Built to be extended with new editors (plug-ins)

Modular, flexible architecture built by application of

numerous design patterns*

*[Erich Gamma, Kent Beck, Contributing to

Eclipse – Principles, Patterns, and Plug-ins, 2004]

MBSE - 2.1 Design Patterns and Architecture 5

Page 6: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Eclipse

Offers common

functionality for

editors

Good starting

point for our class

diagram editor

MBSE - 2.1 Design Patterns and Architecture 6

Page 7: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Class Diagram Editor Design

Continued

How could a class diagram editor be structured?

Typically, there are several design patterns in an

implementation of such an editor.

Let’s look at some examples.

Eclipse

platform

extension points class

diagram

plug-in

MBSE - 2.1 Design Patterns and Architecture 7

Page 8: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Model-View-Controller (MVC)

Paradigm / Architectural Style

Goal: Decouple the view from the model to easily add new

views without having to modify the model

Class

Model Editor

View

Class Diagram EditorClasses

(Model)Figures

(View)

EditParts

(Controller)

2. request

Class Diagram Editor

1. interaction

Command

3. create

4. modifies

5. notification

6. updates

MVC is not really a

design pattern, but an

architectural style

MVC uses several

patterns, e.g. Observer

& CommandMBSE - 2.1 Design Patterns and Architecture 8

Page 9: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Observer Pattern in MVC

On modification of the model,

the model notifies the

controller about the change

(e.g. changed class name)

and the controller redraws the

view

Classes

(Model) Figures (View)

EditParts

(Controller)

register

updates

notification

Subject

notify() : void

ConcreteSubject

Observer

update() : void

ConcreteObserver

update() : void

observers

*

has some

accessible state

uses

subject’s

state

subject

1

call update() on

each observer

Subject Observer

MBSE - 2.1 Design Patterns and Architecture 9

Page 10: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Command Pattern

Example in MVC:

What is the idea behind the pattern?

Classes

(Model)

EditParts

(Controller)

create

Client ReceiverInvoker

Class Diagram Editor

Command

Stack

Command

execute &

modify

MBSE - 2.1 Design Patterns and Architecture 10

Page 11: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Command Pattern –

Motivation

Editor’s palette provides different

operations

Problems:

How can the operations be handled

uniformly?

How can these operations be undone, re-

done?

How can the operations be

parameterized, e.g. with coordinates?

MBSE - 2.1 Design Patterns and Architecture 11

Page 12: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Basic idea: encapsulate each operation

and its parameters in an object and

provide a uniform interface

Command Pattern

CreateClassCommand

CreateAttributeCommand

Command

+ execute()

+ undo()

+ redo()

+ …

+ …

MBSE - 2.1 Design Patterns and Architecture 12

Page 13: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

General Structure: Command

InvokerCommand

+ execute()

ConcreteCommand

- state

execute() {

receiver.action();

...

}

+ execute()

Client

Receiver

+ action() receiver

Consequences

Adding new operations is easy

All operations are handled uniformly

Each operation can undo its action

Parameters and internal state saved in

Command objectMBSE - 2.1 Design Patterns and Architecture 13

Page 14: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Design Pattern: Command

Intent:

Encapsulate actions in (parameterized) objects to ease adding

actions and provide general undo-mechanism

Also known as:

Action, Transaction

Motivation

GUI elements (e.g. buttons) represent operations performed

by the user

Functionality not implemented in the GUI

Command objects encapsulate user operations and can be

undone

Related Pattern:

Composite can be used to implement combined commands

MBSE - 2.1 Design Patterns and Architecture 14

Page 15: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Composite Pattern –

Motivation

Graphical elements in a class diagram are hierarchical and

should be drawn uniformly

Class figure contains

Attribute figures

Method figures

Naive solution: class diagram editor stores references to all

figures and draws each of them by running through the

hierarchy and calling their draw operations

Drawbacks:

Each type of figures has to be handled differently

Draw operations have to be called explicitly on each figure

MBSE - 2.1 Design Patterns and Architecture 15

Page 16: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Composite Pattern

All figures have the same

interface

Some figures can have

contained subfigures

All figures are drawn

uniformly

Each figure is

responsible for drawing

itself and its child figures

AttributeFigure

+ drawFigure() : void

Figure

+ drawFigure() : void

ClassFigure

+ drawFigure() : void

+ add(child : Figure) : void

+ remove(child : Figure) : boolean

+ getChild(index : int) : Figure

public void drawFigure() {

for(Figure f : getChildren()){

f.drawFigure();

}

}

children

*

:ClassFigure

:AttributeFigure :AttributeFigure

:ClassDiagramCanvas

:ClassFigure

:AttributeFigure :MethodFigure

MBSE - 2.1 Design Patterns and Architecture 16

Page 17: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

General Structure: Composite

Leaf

+ operation()

Component

+ operation()

Composite

+ add(component : Component) : Void

+ remove(component : Component) : Boolean

+ getChild(index : int) : Component

+ operation()

Client

operation() {

for(Component c : children) {

c.operation();

}

}

children

MBSE - 2.1 Design Patterns and Architecture 17

Page 18: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Implementation Variants

Clean Variant:

Distinguish leafs from composites(only composites can have children)

Efficient Variant

Completely uniform treatment of all elements, but leafs could have children

Leaf

+ operation()

Component

+ operation()

Composite

+ add(component : Component) : Void

+ remove(component : Component) : Boolean

+ getChild(index : int) : Component

+ operation()

Client

operation() {

for(Component c : children) {

c.operation();

}

}

children

Leaf

+ operation()

Component

+ add(component : Component) : Void

+ remove(component : Component) : Boolean

+ getChild(index : int) : Component

+ operation()

Composite

+ operation()

Client

operation() {

for(Component c : children) {

c.operation();

}

}

children

*

MBSE - 2.1 Design Patterns and Architecture 18

Page 19: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Design Pattern: Composite

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 (see assignments) is used

combined with the Composite Pattern.

The Iterator Pattern can be used to traverse composite

structures.

The Visitor Pattern (see assignments) can be used to

encapsulate behaviour that would otherwise be distributed

across the composites and leafs.

MBSE - 2.1 Design Patterns and Architecture 19

Page 20: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Strategy Pattern – Motivation

Outline view in Eclipse lists all

elements of a class diagram, e.g.

classes, attributes, and methods

List can be sorted differently

sorted

Alphabetically

Order of appearance

Other sorting algorithms could be

added in future

Naive solution: implement sorting

algorithms directly in outline view

Drawbacks: adding or replacing

the algorithms is complicatedMBSE - 2.1 Design Patterns and Architecture 20

Page 21: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Strategy Pattern

OutlineContentProvider

+ sortTree(...)

sortingStrategy Sorting

+ sort(...)

AlphaNumSort

+ sort(...)

sortTree(...) {

sortingStrategy.sort(...);

...

}

AppearanceSort

+ sort(...)

MBSE - 2.1 Design Patterns and Architecture 21

Page 22: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

General Structure: Strategy

Context

+ request()

strategy Strategy

+ algorithm()

ConcreteStrategy

+ algorithm()

request() {

strategy.algorithm();

...

}

MBSE - 2.1 Design Patterns and Architecture 22

Page 23: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Design Pattern: Strategy

Intent:

Encapsulation of a family of dynamically interchangeable

algorithms

Also Known As:

Policy

Problem:

“Best" algorithm can often not be uniquely determined due to

trade-offs:

• Runtime <=> Memory

• Runtime <=> Quality

Best solution depends on problem characteristics: e.g. for

sorting: data structure, stability, problem size, user needs,...

Related Pattern:

Strategy objects often make good flyweights

MBSE - 2.1 Design Patterns and Architecture 23

Page 24: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Basic Concept in Patterns:

Delegation

Used, e.g. in the Strategy pattern

An objects delegates a task to another object

Another example:

Advantages:

Loose coupling, Rectangle class can be replaced at runtime and

Window does not have to subclass Rectangle (it is not a rectangle)

Rectangle

- width : int

- height : int

- x : int

- y : int

Window

+ getPosition() : Point

+ getWidth(): int

+ getHight(): int

bounds

Point getPosition() {

return bounds.getLocation();

}

+ getLocation() : Point

+ ...

Point getLocation() {

return new Point(x, y);

}

1

MBSE - 2.1 Design Patterns and Architecture 24

Page 25: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Abstract Factory Pattern –

Motivation

Class diagram editor is to open two

different formats of class diagrams

Class models differ, but are

conceptionally similar

Naive solution: direct call of the

constructor for each type of class

and its attributes, etc.

Drawbacks: all types of diagram

elements have to be treated

differently, adding new types of

diagrams is difficult

identifier: String

visibility: int

UMLClass RRClass

name: String

visibility: String

UMLAttribute

identifier: String

visibility: int

RRAttribute

name: String

visibility: String

MBSE - 2.1 Design Patterns and Architecture 25

Page 26: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Abstract Factory Pattern

Basic idea: introduce an

interface for similar objects and

an interface for the creation of a

family of

similar objects

Creation operations

defined in a factory

Factory subclasses

determine what to

create (products)

Ensures consistent

creation of product

families and type

secure programs

ClassModelFactory

+ createClass() : Class

+ createAttribute() : Attribute

...

UMLClass

Class

RRClass

UMLAttribute

Attribute

RRAttribute

+ createClass() : Class

+ createAttribute() : Attribute

...

UMLClassFactory RRClassFactory

+ createClass() : Class

+ createAttribute() : Attribute

...

«instantiate»

«instantiate»

«instantiate»

«instantiate»

name: String

visibility: int

name: String

visibility: int

MBSE - 2.1 Design Patterns and Architecture 26

Page 27: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

General Structure:

Abstract Factory

Client AbstractFactory

+ createProductA() : AbstractProductA

+ createProductB() : AbstractProductB

ConcreteProductA1

AbstractProductA

ConcreteProductA2

ConcreteProductB1

AbstractProductB

ConcreteProductB2

+ createProductA() : AbstractProductA

+ createProductB() : AbstractProductB

ConcreteFactory1 ConcreteFactory2

+ createProductA() : AbstractProductA

+ createProductB() : AbstractProductB

«instantiate»

«instantiate»

«instantiate»

«instantiate»

MBSE - 2.1 Design Patterns and Architecture 27

Page 28: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Design Pattern: Abstract

Factory

Intent:

Structure creation of related objects

Problem:

How to manage the creation of families of related or

dependent object?

Solution:

Encapsulate object creation within one class

Redefine in subclasses as required

Related Patterns:

Often a concrete factory is also a singleton

Often realized using the Factory Method Design Pattern

MBSE - 2.1 Design Patterns and Architecture 28

Page 29: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Adapter Pattern – Motivation

The implementation of the different class models to be

opened with your editor cannot be changed, the source

code is not under your control, i.e. you cannot introduce a

common interface for class model elements

E.g. you want to open Ecore class models in your editor and connot introduce a common interface for UMLClass

and EClass objects (you cannot modify EClass)

identifier: String

visibility: int

UMLClass

UMLAttribute

identifier: String

visibility: int

EClass

name: EString

abstract: EBoolean

EAttribute

name: String

MBSE - 2.1 Design Patterns and Architecture 29

Page 30: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Adapter Pattern

Class

+ getClassName(): String

+ getVisibility(): int

+ isAbstract(): boolean

...

EClassAdapter

+ getClassName(): String

...

original

ClassModelFactory

public String getClassName() {

return original.getName();}

className: String

visibility: int

abstract: boolean

...

UMLClass EClass

name: EString

abstract: EBoolean1

Basic idea: convert the available interface into the required

interface by means of an adapter (wrapper) object and

delegation

MBSE - 2.1 Design Patterns and Architecture 30

Page 31: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

General Structure: Adapter

Target

+ request()

Adapter

+ request()

Adaptee

+ specificRequest()

adaptee

Client

request() {

adaptee.specificRequest();

}

MBSE - 2.1 Design Patterns and Architecture 31

Page 32: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Design Pattern: Adapter

Intent:

Transform a call to another call-format needed by other class

Adapt external interfaces (libraries, modules, ...) to the

required internal interfaces

Also Known As:

Wrapper

Related Patterns:

The Bridge is used to separate the interface from 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 interface and keeps the functionality

MBSE - 2.1 Design Patterns and Architecture 32

Page 33: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Bridge Pattern – Motivation

Variance both, on a

conceptional and

implementation level:

Conceptional variance

• Warning dialog

• Info dialog

• Error dialog

Implementation variance

• Platform: e.g. Windows vs. Linux

• GUI framework: e.g. AWT vs. Swing vs. SWT

Naive solution: Represent both dimensions of variance in

one class hierarchy.

Drawback: Abstraction and implementation cannot be

modified or extended independently.

MBSE - 2.1 Design Patterns and Architecture 33

Page 34: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Bridge Pattern

Basic idea: separate concepts and implementations in

separate class hierarchies, use the different implementation

via delegation

WarningDialog

drawOkButton()

Dialog

drawRect(...)

drawText(...)

DecisionDialog

drawYesButton()

drawNoButton()

WinDialogImpl

drawLine(...)

drawText(...)

DialogImpl

drawLine(...)

drawText(...)

LinuxDialogImpl

drawLine(...)

drawText(...)

Abstraction Implementation

impl

impl.drawLine(...)

impl.drawLine(...)

impl.drawLine(...)

impl.drawLine(...)

drawRect()

...

winDrawLine(...)

...

Bridge

MBSE - 2.1 Design Patterns and Architecture 34

Page 35: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Bridge Pattern

Consequences

concepts and implementations can vary quite independently

Adding or changing implementations for different platforms /

frameworks without changing in the rest of the program

MBSE - 2.1 Design Patterns and Architecture 35

WarningDialog

drawOkButton()

Dialog

drawRect(...)

drawText(...)

DecisionDialog

drawYesButton()

drawNoButton()

WinDialogImpl

drawLine(...)

drawText(...)

DialogImpl

drawLine(...)

drawText(...)

LinuxDialogImpl

drawLine(...)

drawText(...)

Abstraction Implementation

impl

impl.drawLine(...)

impl.drawLine(...)

impl.drawLine(...)

impl.drawLine(...)

drawRect()

...

winDrawLine(...)

...

Bridge

Page 36: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

General Structure: Bridge

Abstraction

+ operation()

impl

operation() {

implementation.operationImpl();

}

Implementor

+ operationImpl()

RefinedAbstraction

ConcreteImplementor

+ operationImpl()

MBSE - 2.1 Design Patterns and Architecture 36

Page 37: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Design Pattern: Bridge

Intent:

Decouple abstraction and implementation to vary them independently

Also known as:

Handle / Body

Motivation:

Hierarchy of classes with similar functionality

Separate abstract class hierarchy from implementation class hierarchy

Achieve platform independence using abstract super classes and platform specific subclasses

Related Patterns:

An Abstract Factory may create and configure a Bridge

MBSE - 2.1 Design Patterns and Architecture 37

Page 38: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Class diagram editor is realized as a plug-in

Each plug-in in Eclipse is only allowed to exist once in a

running Eclipse workbench

Plug-ins should be accessible by other plug-ins

Naive solution: global variable to hold the single plug-in

instance

Drawback: no one ensures, there is only one plug-in

instance

Singleton Pattern – Motivation

Eclipse

plugin Class

diagram

plugin

MBSE - 2.1 Design Patterns and Architecture 38

Page 39: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Singleton Pattern

Basic idea:

Single access point to the plug-in instance

Private constructor

Static method provides access and ensures to create only

one instance

public class ClassDiagramPlugin {

private static ClassDiagramPlugin instance;

public static ClassDiagramPlugin get() {if (instance == null) {instance = new ClassDiagramPlugin();

}return instance;

}

private ClassDiagramPlugin() {...

}}

Singleton

MBSE - 2.1 Design Patterns and Architecture 39

ClassDiagramPlugin

- instance : ClassDiagramPlugin

+ get() : ClassDiagramPlugin

- ClassDiagramPlugin()

Exemplary implementation in Java:

Page 40: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

General Structure: Singleton

Singleton

- uniqueInstance : Singleton

- data : SingletonData

+ instance() : Singleton

+ getData() : SingletonData

+ singletonOperation() : void

...

return uniqueInstance;

MBSE - 2.1 Design Patterns and Architecture 40

Page 41: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Design Pattern: Singleton

Intent:

Ensure a class has only one instance

Problem:

How to manage the creation and access to this object?

Solution:

Static access to the instance

Create instance if not present

But! Depending on the platform or programming language it is

difficult to always ensure, there is only one instance, e.g. in

Java, if concurrent execution or multiple virtual machines are

involved, another implementation of the pattern is needed*.

Related Patterns:

Often a concrete factory is also a singleton*See e.g.: http://www.ibm.com/developerworks/java/library/j-dcl/index.html

MBSE - 2.1 Design Patterns and Architecture 41

Page 42: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Creational Design Patterns

Motivation

Abstract from instantiation process

Make system independent of object creation

Details:

What class? Who creates? How? When?

Problems:

Class creation spread over the code (no single point of

change)

Basic themes:

Encapsulate knowledge about concrete classes

Hide how objects are created and composed

Presented here:

Singleton and Abstract Factory

MBSE - 2.1 Design Patterns and Architecture 42

Page 43: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Structural Patterns

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

Presented here:

Composite Pattern, Bridge Pattern, Adapter Pattern

MBSE - 2.1 Design Patterns and Architecture 43

Page 44: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Behavioral Patterns

Motivation:

Assign responsibilities to different objects to encapsulate

operations and make the operations replaceable and

extendable

Problems:

How to change the behaviour in different classes at runtime?

Presented here:

Observer Pattern (last week)

Strategy Pattern

Command Pattern

MBSE - 2.1 Design Patterns and Architecture 44

Page 45: 2.1 Design Patterns and Architecture (continued) · n The 23 Gang-of-Four (GoF) Design Patterns Gang-of-Four = authors of the design patterns book: Erich Gamma, Richard Helm, Ralph

© F

achgebie

t S

oft

ware

technik

, H

ein

z N

ixdorf

Institu

t, U

niv

ers

ität P

aderb

orn

Discussion & Summary

Design patterns enable reuse of design experience at the design level!

Each design pattern application should be guided by analyzing the trade-offs

Design patterns are domain-independent concepts, but their application in a specific context is guided by the domain-specific requirements (e.g. time, space)

Design patterns generalize concepts that have been successfully employed in real-world applications and therefore are ”bullet proofed”

Design patterns characterize “good” designs (well-defined class hierarchies and object interactions)

Beware! Design patterns are no silver bullets!

Only use them when it makes sense.

MBSE - 2.1 Design Patterns and Architecture 45