a calculus of design patterns hong zhu dept. of computing and electronics oxford brookes university...

47
A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: [email protected]

Upload: riley-stafford

Post on 28-Mar-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

A Calculus of Design Patterns

Hong ZhuDept. of Computing and ElectronicsOxford Brookes UniversityOxford OX33 1HX, UkEmail: [email protected]

Page 2: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 2Aug. 2010

Acknowledgement Ian Bayley and Hong Zhu, A formal

Language of Pattern Composition, Proc. of PATTERNS 2010, Nov. 2010. (in press)

Hong Zhu and Ian Bayley, Laws of Pattern Composition, Proc. of ICFEM 2010, Nov. 2010. (in press)

Ian Bayley and Hong Zhu, Formal specification of the variants and behavioural features of design patterns, Journal of Systems and Software Vol.83, No.2, pp209–221 (Feb 2010)

Page 3: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 3Aug. 2010

Overview Introduction Operators on patterns Case study 1: The expressiveness

GoF suggested compositions Laws of pattern composition Case study 2: the proof of equivalence

Representation of composition with overlaps Soundness of the operators Future work

Page 4: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 4Aug. 2010

Introduction Software Design Patterns (DP)

codified reusable solutions to recurring design problems,

increasingly important role in the development of software systems

Research on DP identified and catalogued (e.g. Gang of Four book) formally specified (Mik 1998, Taibi 2003, Gasparis et

al. 2008, Bayley&Zhu 2009) included in software tools (e.g. PINOT, Mapelsden et

al. 2002)

Page 5: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 5Aug. 2010

Formal Specification of DPs Formal approaches

Eden's approach• Formal logic predicates on the structural features of the source code

of object-oriented programs (Gasparis et al. 2008) Taibi's approach

• Formal logic predicates on the structural features of the source code • Temporal logic statements for behavioural features

Our predicates (Bayley & Zhu, 2007, 2008, 2009)• Formal logic predicates for both structural and behavioural features

of designs in UML class and sequence diagrams• The primitive predicates are induced from GEBNF definition of UML

Semi-formal approaches Uses of graphic meta-modelling languages For example: RBML, DPML, PDL, etc.

extension of BNF for graphical modelling languages

Page 6: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 6Aug. 2010

Example: The Object Adapter DP In GoF book:

Indicate where messages can be sent to rather than a component of the pattern!

In general, there can be a set of such methods!

Page 7: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 7Aug. 2010

Formal Spec of the Object Adapter DP

Page 8: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 8Aug. 2010

Example 2: The Composite DP GoF Diagram:

In general, there can be many leaves!

Page 9: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 9Aug. 2010

Spec of the Composite DP

Page 10: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 10Aug. 2010

General Definition In general, a design pattern P can be defined abstractly

as an ordered pair <V, Pr>, where Pr is a predicate on the domain of some representation of

software systems. It specifies the structural and behavioural features of the pattern

V is a set of declarations of variables free in Pr. It specifies the components of the pattern.

Notations: Let V = { v1:T1, …, vn:Tn } The semantics of the specification is a ground predicate in the

form:

v1:T1 … vn:Tn . (Pr) Spec(P) to denote the predicate above, Vars(P) for the set of variables declared in V, and Pred(P) for the predicate Pr. m P : a design model m conforms to pattern P

vi are variables that range over the type Ti of software elements.

Page 11: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 11Aug. 2010

Pattern Composition Why compose patterns

Patterns are usually to be found composed with each other in real applications

In practice: Annotation:Role representation: In graphic diagram

Taibi’s work 2006 pattern composition is a combination of formal statements of

structural and behavioural features of patterns Illustrated by an example on how two patterns can be composed

Bayley &Zhu, 2008 One DP composition operator with a notion of overlaps, which

can be• One-to-One overlaps• One-to-Many overlaps• Many-to-many overlaps

Page 12: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 12Aug. 2010

Example of Pattern Composition

Page 13: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 13Aug. 2010

Overview of Our Approach We define a set of six operators on DPs so that

compositions of DPs can be formally represented as expressions Restriction: P Pr P Superposition: P P P Extension: P V Pr P Flatten: P V P Generalisation: P V P Lift: P V P

We propose a set of algebraic laws to reason about the equivalence of DP compositions (i.e. expressions)

Page 14: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 14Aug. 2010

Restriction Operator P[c] Let P be a given pattern and c be a

predicate defined on the components of P. A restriction of P with constraint c, written as P[c], is the pattern obtained from P by imposing the predicate c as an additional condition on the pattern. Formally,

Page 15: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 15Aug. 2010

Example

A variant of the Composite pattern in which there is only one leaf can be formally defined as follows.

Composite1 = Composite [ ||Leaves|| = 1]

called Composite1 in the sequel

Page 16: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 16Aug. 2010

Superposition Operator P * Q Let

P and Q be two patterns. the component variables of P and Q are disjoint, i.e.,

Vars(P) Vars(Q) = . Definition:

The superposition of P and Q, written P * Q, is a pattern that consists of both pattern P and pattern Q as is formally defined as follows.

Page 17: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 17Aug. 2010

Example The superposition of Composite and Adapter

patterns: Composite * Adapter

It requires each instance to contain one part that satisfies the Composite pattern and another that satisfies the Adapter pattern.

These parts may or may not overlap, The following expression does enforce an overlap,

requiring that a class in Leaves of the Composite be the target of an Adapter.

Page 18: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 18Aug. 2010

Extension Operator P#(V c) Let

P be a pattern, V be a set of variable declarations that are disjoint

with P's component variables, i.e.,

Vars(P) V = c be a predicate with variables in Vars(P) V.

Definition:The extension of pattern P with components V and

linkage condition c, written as P#(V c), is defined as follows.

Page 19: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 19Aug. 2010

Flatten Operator P x\ x' Let

P be a pattern, Vars(P)= {x: P(T), x1:T1, …, xk:Tk},

Pred(P)=p(x,x1,…,xk), and x' Vars(P).

Definition:The flattening of P on variable x, written Px\ x',

is the pattern that has the following property.

where

the power set of T

We also write Px

Page 20: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 20Aug. 2010

Example The single-leaf variant of the Composite

pattern Composite1 can also be defined as follows.

Composite1 = Composite Leaves\ Leaf

Page 21: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 21Aug. 2010

Notations From the definition, we have the following

property. For x1 x2 and x1’ x2’,

We can overload the operator to a set of component variables Let X be a subset of P's component variables all of

power set type, i.e., X={x1:P(T1), …, xn:P(Tn)} Vars(P), n 1X'={x'1, …, x'n} X' Vars(P) = .

Notation: P X \ X' to denote P x1\ x'1 … xn\ x'n.

We also write PX

Page 22: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 22Aug. 2010

Generalisation Operator P x\x' Let

P be a pattern, x Vars(P)={x:T, x1:T1, …, xk:Tk}.

Definition:The generalisation of P on variable x,

written P x\x', is defined as follows.

Notation: Similar to the operator, we also write P x, and

P X

Page 23: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 23Aug. 2010

Example We can define the Composite pattern as a

generalisation of the single-leaf variant Composite1:

Composite = Composite1 Leaf \ Leaves

Page 24: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 24Aug. 2010

Lift Operator Let

P be a pattern CVars(P)={x1:T1, …, xn: Tn}, n>0

OPred(P)=p(x1, …, xn).

X={x1, …, xk}, 1 k < n, be a subset of the variables in the pattern.

Definition:The lifting of P with X as the key, written PX, is the

pattern defined as follows.

the existentially quantified class components

the remainder of the predicate

Page 25: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 25Aug. 2010

Example

Page 26: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 26Aug. 2010

Example

Notation:• P[x' := x]: systematically renaming x to x’;• P[v:= x=y]: the syntactic sugar for P[x=y][v:= x][v:=y];• P[v:= x y]: abbreviates P[x y][v:= x]

Page 27: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 27Aug. 2010

Case Study Goal:

to demonstrate the expressiveness of the operators Subjects of the case study:

In the GoF book, the documentation for each pattern concludes with a brief section entitled Related Patterns.

• It compares and contrasts patterns, • it makes suggestions for how other patterns may be used with the

one under discussion. Example: page 106 of the GoF book

“A Composite is what the builder often builds''. This can be formally specified as follows.

(Builder * Composite) [Product = Component]. Method:

to formalise them all as expressions with • The operators from this paper • Formal logic predicates specifying the patterns in our previous work

(Bayley&Zhu 2009)

Page 28: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 28Aug. 2010

Results of The Case Study

Page 29: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 29Aug. 2010

Page 30: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 30Aug. 2010

Summary of Case Study Results Five new arrows have been added to the GoF diagram

(numbered in bold font) discussed in GoF main text but omitted from its diagram We were able to formalise them

Four arrows from the original diagram were not formalised (labelled with asterisks) Composite and Interpreter: is a specialisation relation, which can

be formally proved; see (Bayley & Zhu 2007). Decorator and Strategy: is a comparison of the two, not a

composition suggestion, Strategy and Template Method: as above. Iterator and Visitor: it is mentioned in GoF only on the diagram,

but not expanded upon in the main text.

The case study has demonstrated that the operators defined in this paper are expressive to define compositions of design patterns.

Page 31: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 31Aug. 2010

Algebraic Laws of the OperatorsLet P and Q be design patterns. Definition: (Specialisation relation)

Let P and Q be design patterns. Pattern P is a specialisation of Q, written P Q, if for all models m, whenever m conforms to P, then, m also conforms to Q.

Definition: (Equivalence relation)Pattern P is equivalent to Q, written P = Q, if P Q and Q P.

Lemma:

m |= P

Page 32: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 32Aug. 2010

The TRUE and FALSE Patterns Definition:

Pattern TRUE is the pattern such that for all models m, m |= TRUE.

Pattern FALSE is the pattern such that for no model m, m |= FALSE.

LemmaFor all patterns P, Q and R, we have that

Page 33: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 33Aug. 2010

Laws of Restriction Let vars(p) denote the set of free variables in a predicate p. For all predicates c, c1, c2 such that vars(c), vars(c1) and

vars(c2) Vars(P), the following equalities hold.

Page 34: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 34Aug. 2010

Laws of Superposition For all patterns P and Q, we have the following equations.

From this and reflexivity of , it follows that superposition is idempotent.

TRUE and FALSE are the unit and zero of superposition.

Superposition is also commutative and associative

Page 35: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 35Aug. 2010

Laws of Extension Let

U be any set of component variables that is disjoint to Vars(P), and c1, c2 be any given predicates such that vars(ci) Vars(P) U,

i=1,2.

Let U and V be any sets of component variables that are disjoint to

Vars(P) and to each other, c1 and c2 be any given predicates vars(c1) Vars(P) U and vars(c2) Vars(P) V.

Page 36: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 36Aug. 2010

Laws of Flattening and Generalisation

Page 37: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 37Aug. 2010

Laws Connecting Several Operators For all predicates c such that vars(c) Vars(P),

For all X Vars(P),

For all X Vars(P) Vars(Q),

where

Page 38: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 38Aug. 2010

For all sets of variables X such that

X vars(P) =, and all predicates c such that

vars(c) (Vars(P) X),

where

For all x Vars(P) such that x : P(T)

Page 39: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 39Aug. 2010

Page 40: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 40Aug. 2010

where

Page 41: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 41Aug. 2010

Example of Proving Equivalence (1) We have seen the following as examples of the

operators: Composite1 = Composite [ ||Leaves|| = 1]

Composite1 = Composite Leaves\ Leaf

Composite = Composite1 Leaf \ Leaves

We can prove that: Composite [ ||Leaves|| = 1]= Composite Leaves\ Leaf Composite= (Composite Leaves\ Leaf) Leaf \ Leaves Composite1=(Composite1 Leaf \ Leaves) Leaves\ Leaf

Page 42: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 42Aug. 2010

Proof of Composite [ ||Leaves|| = 1] = Composite Leaves\ Leaf

For all sets X, we have that

||X||=1 x(X={x})

Therefore,

Composite[ ||Leaves||=1]

= Composite [ Leaf (Leaves={Leaf}) ]

= Composite # (Leaf Leaves={Leaf})

= Composite Leaves \ Leaf

Page 43: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 43Aug. 2010

Example of Proving Equivalence (2) Two ways of composition of the Composite

and the Adapter patternsExpress the compositions as expressions that

consist of the operatorsApply the algebraic laws to prove that they

are equivalent

Page 44: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 44Aug. 2010

First Way of Composition One of the Leaves in the Composite pattern is the Target in

the Adapter pattern. This leaf is renamed as the AdaptedLeaf.

Lift the adapted leaf to enable several of these Leaves to be adapted. lift the OneAdaptedLeaf pattern with AdaptedLeaf as the key flatten those components in the composite part of the pattern

(i.e. the components in the Composite pattern remain unchanged).

Page 45: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 45Aug. 2010

Page 46: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 46Aug. 2010

Second Way of Composition Lift the Adapter with target as the key Superpose it to the Composite patterns

We can prove that the two ways are equivalent by applying the algebraic laws.

Page 47: A Calculus of Design Patterns Hong Zhu Dept. of Computing and Electronics Oxford Brookes University Oxford OX33 1HX, Uk Email: hzhu@brookes.ac.uk

Seminar: A Calculus of Design Patterns 47Aug. 2010

Conclusion Work in progress and future work

The uses of theorem provers for automated reasoning about the compositions of design patterns

The soundness of the algebraic lawsThe completeness of the algebraic laws