design patterns - formal.iti.kit.edubeckert/teaching/praktikum-formale... · design patterns:...

Post on 03-Apr-2019

216 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Entwic klung objektorientier ter Software mit formalen Methoden

Design Patterns

Bernhar d Becker t

UNIVERSITÄT KOBLENZ-LANDAU

KeY-Praktikum WS03/04 � ��� �

Design Patterns: Libraries for design

Collection of solutions for common software design problems

Make experiences of designer s availab le to other s

better education

Prevent software engineer s from searching for alread y found solutions

increase of effectiveness

Suppor t comm unication between developer s

easier orientation

Document design decisions of a software system

increase of maintenance

KeY-Praktikum WS03/04 � ��� �

Design Patterns: Libraries for design

Collection of solutions for common software design problems

Make experiences of designer s availab le to other s

� better education

Prevent software engineer s from searching for alread y found solutions

increase of effectiveness

Suppor t comm unication between developer s

easier orientation

Document design decisions of a software system

increase of maintenance

KeY-Praktikum WS03/04 � ��� �

Design Patterns: Libraries for design

Collection of solutions for common software design problems

Make experiences of designer s availab le to other s

� better education

Prevent software engineer s from searching for alread y found solutions

� increase of effectiveness

Suppor t comm unication between developer s

easier orientation

Document design decisions of a software system

increase of maintenance

KeY-Praktikum WS03/04 � ��� �

Design Patterns: Libraries for design

Collection of solutions for common software design problems

Make experiences of designer s availab le to other s

� better education

Prevent software engineer s from searching for alread y found solutions

� increase of effectiveness

Suppor t comm unication between developer s

� easier orientation

Document design decisions of a software system

increase of maintenance

KeY-Praktikum WS03/04 � ��� �

Design Patterns: Libraries for design

Collection of solutions for common software design problems

Make experiences of designer s availab le to other s

� better education

Prevent software engineer s from searching for alread y found solutions

� increase of effectiveness

Suppor t comm unication between developer s

� easier orientation

Document design decisions of a software system

� increase of maintenance

KeY-Praktikum WS03/04 � ��� �

Description scheme of design patterns

Motiv ation

Where does the need for this pattern come from?

Structure

What is the concrete (class) structure of the pattern?

Applicability

Under whic h cir cumstances is it applicab le?

Whic h are the forces it obeys?

Consequences

What are the effects when using this pattern?

KeY-Praktikum WS03/04 � ��� �

Description scheme of design patterns

Motiv ation

Where does the need for this pattern come from?

Structure

What is the concrete (class) structure of the pattern?

Applicability

Under whic h cir cumstances is it applicab le?

Whic h are the forces it obeys?

Consequences

What are the effects when using this pattern?

KeY-Praktikum WS03/04 � ��� �

Description scheme of design patterns

Motiv ation

Where does the need for this pattern come from?

Structure

What is the concrete (class) structure of the pattern?

Applicability

Under whic h cir cumstances is it applicab le?

Whic h are the forces it obeys?

Consequences

What are the effects when using this pattern?

KeY-Praktikum WS03/04 � ��� �

Description scheme of design patterns

Motiv ation

Where does the need for this pattern come from?

Structure

What is the concrete (class) structure of the pattern?

Applicability

Under whic h cir cumstances is it applicab le?

Whic h are the forces it obeys?

Consequences

What are the effects when using this pattern?

KeY-Praktikum WS03/04 � ��� �

Impr oving Consistence and Reusability

Model View Contr oller

Mo FrWeTu Th

Mo FrWeTu Th

Mo FrWeTu Th

Mo FrWeTu Th

Novembre 03

Month View

Mo. 10. Nov.8

17

109

...

Day View

Organizer Data

Database

Keeping Model, View and Contr oller separate allo ws

� diff erent consistence views

� reuse of views in other conte xts

� dynamic chosen response

KeY-Praktikum WS03/04 � ���

The »Obser ver«: Separates model and view

Purpose: Defines a 1-to-n dependance relationship between objects, so

that the state chang e of one object causes a notification of the

dependant objects.

Applicability:

If the state chang e of one object, requires to update several other

objects.

If an object has to inf orm several other unkno wn objects.

Consequence:

Independant reuse of subject and obser ver.

Obser vers can be added without changing the obser ver or subject.

KeY-Praktikum WS03/04 � ���

The »Obser ver«: Separates model and view

Purpose: Defines a 1-to-n dependance relationship between objects, so

that the state chang e of one object causes a notification of the

dependant objects.

Applicability:

� If the state chang e of one object, requires to update several other

objects.

� If an object has to inf orm several other unkno wn objects.

Consequence:

Independant reuse of subject and obser ver.

Obser vers can be added without changing the obser ver or subject.

KeY-Praktikum WS03/04 � ���

The »Obser ver«: Separates model and view

Purpose: Defines a 1-to-n dependance relationship between objects, so

that the state chang e of one object causes a notification of the

dependant objects.

Applicability:

� If the state chang e of one object, requires to update several other

objects.

� If an object has to inf orm several other unkno wn objects.

Consequence:

� Independant reuse of subject and obser ver.

� Obser vers can be added without changing the obser ver or subject.

KeY-Praktikum WS03/04 � ���

The class structure of the obser ver pattern

��� ���� �

��� ��� � � � ����� ��� ��

��!" # � � ���� ��� ��

$" � �%�& �

' �( �) *�)

+, -�. � � �

/�01 � ) � �� � � ���� �

2 � 3 4 35 3 � 67

� � 3 4 35 3 � 67

/�01 � ) � �� ' �( �) *�)

8 9 :5 3 � 67

;�< � 5 = = < ��� ��� ��� >

� �? : >A@ 8 9 :5 3 � 67

B C�DEF GEFH

Two modes:

pull : the obser ver has to ask for the new subject state ( )

push : chang e inf ormation is handed over to the obser ver when

notifying

KeY-Praktikum WS03/04 � ��� I

The class structure of the obser ver pattern

J�K LM�NO P

QRS T�U V R Q WXY�Z [\] [\ ^

QR_` a R W XY�Z [\] [\ ^

b` V Tc�d W ^

e L�f Ng hNg

ij k�l V R W ^

m�no O g N PN J K LM�NO P

p [ q r qs q [ tu

Z [ q r qs q [ tu

m�no O g N PN e L�f Ng hNg

v w xs q [ tu

y�z \ s { { z Y�Z [\] [\Z |

Z [} x |A~ v w xs q [ tu

� ����� ����

Two modes:

� pull : the obser ver has to ask for the new subject state ( �� � � �� � � )

push : chang e inf ormation is handed over to the obser ver when

notifying

KeY-Praktikum WS03/04 � ��� I

The class structure of the obser ver pattern

��� ����� �

��� ��� � � � ����� ��� �� �

�� ¡ ¢ � � ���� ��� �� �

£¡ � �¤�¥ � �

¦ ��§ �¨ ©�¨

ª« ¬�­ � � � �

®�¯° � ¨ � �� � � ����� �

± � ² ³ ²´ ² � µ¶

� � ² ³ ²´ ² � µ¶

®�¯° � ¨ � �� ¦ ��§ �¨ ©�¨

· ¸ ¹´ ² � µ¶

º�» � ´ ¼ ¼ » ��� ��� ��� ½

� �¾ ¹ ½A¿ · ¸ ¹´ ² � µ¶

À Á�ÂÃÄ ÅÃÄÆ

Two modes:

� pull : the obser ver has to ask for the new subject state ( �� � � �� � � )

� push : chang e inf ormation is handed over to the obser ver when

notifyingKeY-Praktikum WS03/04 � ��� I

Update of views implementing an obser ver

Ç ÈÉ Ê�Ë ÌÍÎ

ÏÐ Ñ�ÒÓÔ ÕÖ × Ö Ø�Ù ÚÛ ÖÔ × ÏÐ Ñ�ÒÓÔ ÕÖ × Ö Ü Ú�Ý Ö ÕÞ Ö Õ ß ÏÐ Ñ�ÒÓÔ ÕÖ × Ö Ü Ú�Ý Ö ÕÞ Ö Õ à

Ë á ÈÉ âã

É Í Ì ä åçæ èéêë ì È Ì ã è é

âã Ì í Ì È Ì ã è é

êë ì È Ì ã è é

âã Ì í Ì È Ì ã èé

KeY-Praktikum WS03/04 � ��� î

Update of views implementing an obser ver

ï ðñ ò�ó ôõö

÷ø ù�úûü ýþ ÿ þ � � �� þü ÿ ÷ø ù�úûü ýþ ÿ þ � ��� þ ý� þ ý � ÷ø ù�úûü ýþ ÿ þ � ��� þ ý� þ ý

ó ðñ ��

ñ õ ô ��� ���� � ð ô � � �

�� ô � ô ð ô � � �

�� � ð ô � � �

�� ô � ô ð ô � ��

KeY-Praktikum WS03/04 � ��� î

Update of views implementing an obser ver

� �� ��� �� �

� !�" #$ %& ' & (�) *+ & $ ' � !�" #$ %& ' & , *�- & %. & % / � !�" #$ %& ' & , *�- & %. & % 0

� 1 �� 23

� � � 4 5�6 789: ; � � 3 7 8

23 � < � � � 3 7 8

9: ; � � 3 7 8

23 � < � � � 3 78

KeY-Praktikum WS03/04 � ��� î

Update of views implementing an obser ver

= >? @�A BC D

EF G�H IJ KL M L N�O PQ L J M EF G�H IJ KL M L R P�S L KT L K U EF G�H IJ KL M L R P�S L KT L K V

A W >? XY

? C B Z [�\ ]^_` a > B Y ] ^

XY B b B > B Y ] ^

_` a > B Y ] ^

XY B b B > B Y ]^

KeY-Praktikum WS03/04 � ��� î

Example: Views of an organiz er

Mo FrWeTu Th

Mo FrWeTu Th

Mo FrWeTu Th

Mo FrWeTu Th

Novembre 03

Month View

Mo. 10. Nov.8

17

109

...

Day View

Organizer Data

Database

c�d efhg i j

kl m nho p l k qr sht uv w uv x

kl yz { l q r sht uv w u v x

| z p n} ~ q x

� eh� g �� g �

�� �h� p l q x

� �� �� �h� g � ��

� � �� � � v � �� � � u � � � u�� � � v � �� � u t �v �

� u � � � � v � u t �� � � u � v �� � � � � u � � �

� ��   �g ¡

¢£ � � � u � �

¤h¥ � j¦   �g ¡

¢£ � � � u � �

§ ¨h©ª« ¬ª«­

KeY-Praktikum WS03/04 � ��� ®

Updating the organiz er’s views

¯�°± ²�³ ´�µ± ¶·¸

¹ º�»¼ ½�¾ ¿�À Á�Âà ¾ Ä Å�Ƽ  Ǿ È ÉÊ É É À Ë�¾ È Ì Å¼ Í Ä ¹Î ¼ Ï Ð¼  À Ñ ¹ Ä Ò Ó Ì Å¼ Í

° Ô ÔÕ ¸ Ö± × Ø¸ µÙ Ú ° Ö µÜÛ ÝÞ ° ß�à ·á ± ¶µ â 㸠· Ö ä å × Ø ã

á æ Ô° Ö µ Ø ã

çµ Ö Õ ¸ Ö± äµ ¶ Øèé êìëé é èÛ è é êëé é è ãá æ Ô° Ö µ Ø ã

çµ Ö Õ ¸ Ö± äµ ¶ Øè êé ëé é èÛ èí êëé é è ã

KeY-Praktikum WS03/04 � ��� î

The »Command«: Actions as objects

Purpose: Encapsulate a command as an object. Allo ws to keep trac k of

actions (transactions).

Applicability:

parameterise clients,

log commands,

realise undo functionality or

cache commands in a queue in order to execute them at a cer tain

time in the future

KeY-Praktikum WS03/04 � �� �ï

The »Command«: Actions as objects

Purpose: Encapsulate a command as an object. Allo ws to keep trac k of

actions (transactions).

Applicability:

� parameterise clients,

log commands,

realise undo functionality or

cache commands in a queue in order to execute them at a cer tain

time in the future

KeY-Praktikum WS03/04 � �� �ï

The »Command«: Actions as objects

Purpose: Encapsulate a command as an object. Allo ws to keep trac k of

actions (transactions).

Applicability:

� parameterise clients,

� log commands,

realise undo functionality or

cache commands in a queue in order to execute them at a cer tain

time in the future

KeY-Praktikum WS03/04 � �� �ï

The »Command«: Actions as objects

Purpose: Encapsulate a command as an object. Allo ws to keep trac k of

actions (transactions).

Applicability:

� parameterise clients,

� log commands,

� realise undo functionality or

cache commands in a queue in order to execute them at a cer tain

time in the future

KeY-Praktikum WS03/04 � �� �ï

The »Command«: Actions as objects

Purpose: Encapsulate a command as an object. Allo ws to keep trac k of

actions (transactions).

Applicability:

� parameterise clients,

� log commands,

� realise undo functionality or

� cache commands in a queue in order to execute them at a cer tain

time in the future

KeY-Praktikum WS03/04 � �� �ï

The class structure of the command pattern

ðñò óô õ ð�ö ñ ñ ó÷

ð�ø ù ùö ô ú

ûü ûýþ ÿ û ��

ðø ô �÷ ó õ ó ðø ù ùö ô ú � ��

�� �� � ��

ð�ø ô �÷ ó õ ó ðø ù ùö ô ú � �

�� �� � ��

� ó � óò � ó÷

�ý ÿ� �� �� ��� � ��� � �

�� � � � � � �

Call of on invokes the receiver’ s operation.

Not necessar y each subc lass invokes same operation.

How could the pattern be altered to avoid encoding the receiver associ-

ation as attrib ute?

KeY-Praktikum WS03/04 � ��� � �

The class structure of the command pattern

� ! "# $ ��% "& ��' ( (% # )

*+ *,- . * /0

�' # 1& " $ " �' ( (% # )32 45

67 689 : 6 ;<

��' # 1& " $ " �' ( (% # )32 =5

67 689 : 6 ;<

> " 1 "! ? "&

@, .A�B C /0 DEF E G�H E D

DE F E G H E D

Call of IJ IK L M I NO

on

PRQ S S TU Vinvokes the receiver’ s TK M W Q U NO

operation.

Call of on invokes the receiver’ s operation.

Not necessar y each subc lass invokes same operation.

How could the pattern be altered to avoid encoding the receiver associ-

ation as attrib ute?

KeY-Praktikum WS03/04 � ��� � �

The class structure of the command pattern

XYZ [\ ] X�^ Y Y [_ X�` a a^ \ b

cd cef g c hi

X` \ j_ [ ] [ X` a a^ \ b3k lm

no npq r n st

X�` \ j_ [ ] [ X` a a^ \ b3k um

no npq r n st

v [ j [Z w [_

xe gy�z { hi |}~ } ��� } |

|} ~ } � � } |

Call of IJ IK L M I NO

on

PRQ S S TU Vinvokes the receiver’ s TK M W Q U NO

operation.

Diff erent

PRQ S S TU V

subc lasses may invokes diff erent TK M W Q U NO

operations.

Not necessar y each subc lass invokes same operation.

How could the pattern be altered to avoid encoding the receiver associ-

ation as attrib ute?

KeY-Praktikum WS03/04 � ��� � �

The class structure of the command pattern

��� �� � ��� � � �� ��� � �� � �

�� ��� � � ��

�� � �� � � � �� � �� � �3� ��

�� ��� � � ��

��� � �� � � � �� � �� � �3� ��

�� ��� � � ��

� � � ��   ��

¡� �¢�£ ¤ �� ¥¦§ ¦ ¨�© ¦ ¥

¥¦ § ¦ ¨ © ¦ ¥

Call of IJ IK L M I NO

on

PRQ S S TU Vinvokes the receiver’ s TK M W Q U NO

operation.

Not necessar y each

PRQ S S TU Vsubc lass invokes same TK M W Q U NO

operation.

How could the pattern be altered to avoid encoding the receiver associ-

ation as attrib ute?

KeY-Praktikum WS03/04 � ��� � �

Execution of a command

General

ª

KeY-Praktikum WS03/04 � ��� �«

Example: Timer trig gered actions

One feature of the organiz er in the former examples is to execute

actions, when a cer tain date (time) has been reached.

For example:

¬ beep 5 min utes before a meeting

¬ send to all par ticipants of a meeting an invitation e-mail a week

before it is scheduled

¬ recor d a TV film

KeY-Praktikum WS03/04 ­ ®�¯ °±

Example (cont’ d): Timer trig gered actions

²³ ²´µ ¶ ²·¸

¹º ¹» ¼ ½ ¹ ¾¿ ¹º ¹» ¼ ½ ¹ ¾¿ ¹º ¹» ¼ ½ ¹ ¾¿

À ¹ ¹Á ¾¿  ¹Ã ÄÅ Æ ÇÈ ¾ÊÉ É É ¿ Á È ÆË ¾¿Ì ¹» Í Ì Ä ¾¿

Î

Ï ÐÑ Ð Ò�Ó Ð Ï Ï ÐÑ Ð Ò�Ó Ð Ï Ï ÐÑ Ð Ò Ó Ð Ï

KeY-Praktikum WS03/04 ­ ®�¯ °Ô

Literature

Õ »Design Patterns: Elements of Reusab le Object-Oriented Software«,by E. Gamma, R. Helm, R. Johnson and J. Vlissides,Addison-W esley (1995)

Õ Lecture Notes »Softwaretec hnik«,W.F. Tichy and G. Goos (WS 1998/99)

Õ Lecture Notes »Design Patterns Overview«,Rob Kremer (2002),Ö× × ØÙ Ú ÚÜÛ ÝÞ ßáà â ãä åçæ ä Þ è à ãä Ú ãé âÞ Û ÝÛ Úê ëì í Úî î ï Úð ñò Ú Øä × × Ý Þ ßÛ Úó ß ô Ýõ à Ö× ö å

KeY-Praktikum WS03/04 ­ ®�¯ °÷

Design Patterns ...

Libraries For Designing System

document design solutions on an abstract level

prevent reinvention of the wheel

suppor t comm unication between developer s

make exper t kno wledg e usab le for beginner s

but:

They don’t make your design decisions.

KeY-Praktikum WS03/04 ­ ®�¯ °ø

Design Patterns ...

Libraries For Designing System

ù document design solutions on an abstract level

prevent reinvention of the wheel

suppor t comm unication between developer s

make exper t kno wledg e usab le for beginner s

but:

They don’t make your design decisions.

KeY-Praktikum WS03/04 ­ ®�¯ °ø

Design Patterns ...

Libraries For Designing System

ù document design solutions on an abstract level

ù

prevent reinvention of the wheel

suppor t comm unication between developer s

make exper t kno wledg e usab le for beginner s

but:

They don’t make your design decisions.

KeY-Praktikum WS03/04 ­ ®�¯ °ø

Design Patterns ...

Libraries For Designing System

ù document design solutions on an abstract level

ù

prevent reinvention of the wheel

ù suppor t comm unication between developer s

make exper t kno wledg e usab le for beginner s

but:

They don’t make your design decisions.

KeY-Praktikum WS03/04 ­ ®�¯ °ø

Design Patterns ...

Libraries For Designing System

ù document design solutions on an abstract level

ù

prevent reinvention of the wheel

ù suppor t comm unication between developer s

ù make exper t kno wledg e usab le for beginner s

but:

They don’t make your design decisions.

KeY-Praktikum WS03/04 ­ ®�¯ °ø

Design Patterns ...

Libraries For Designing System

ù document design solutions on an abstract level

ù

prevent reinvention of the wheel

ù suppor t comm unication between developer s

ù make exper t kno wledg e usab le for beginner s

but:

They don’t make your design decisions.

KeY-Praktikum WS03/04 ­ ®�¯ °ø

top related