Design Patterns I - Introduction: Christopher Alexander's...

32
Design Patterns I Alexei Khorev Design and patterns Designs humans do Architectural design Christopher Alexander Great architecture What is DP? Patterns in Software Patterns and Idioms DPs for Software DP Misconceptions 15.1 Design Patterns I Introduction: Christopher Alexander’s Vision COMP2110/2510 Software Design Software Design for SE September 10, 2008 Alexei Khorev Department of Computer Science The Australian National University

Transcript of Design Patterns I - Introduction: Christopher Alexander's...

Page 1: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.1

Design Patterns • IIntroduction: Christopher Alexander’s Vision

COMP2110/2510Software DesignSoftware Design for SESeptember 10, 2008

Alexei Khorev

Department of Computer Science

The Australian National University

Page 2: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.2

1 Design and patterns

2 Designs humans do

3 Architectural design

4 Christopher Alexander

5 Great architecture

6 What is DP?

7 Patterns in Software

8 Patterns and Idioms

9 DPs for Software

10 DP Misconceptions

Page 3: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.3

Design and patterns

What “design” means?

1 plan or drawing produced to show the look and function orworkings of a building, garment, or other object before it isbuilt or made

2 purpose, planning, or intention that exists or is thought toexist behind an action, fact, or material object

What “pattern” means?

1 an arrangement or sequence regularly found in comparableobjects or events

2 a regular and intelligible form or sequence discernible incertain actions or situations

3 a model or design used as a guide in needlework and othercrafts (which infers that software design is a craft)

Design and patterns are thus related. ,

Page 4: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.4

Design as human activity

Design activitities humans are involved in

• In civil engineering — buildings, bridges, roads,. . .

(static constructions)

• In mechanical engineering — vehicles of every kind

(dynamic constructions)

• In electrical engineering — electrical/electronic devices

• In architecture (same objects as in civil engineering,

but a different aspect)

• In industrial design in broad sense (includes HCI)

• In political and social engineering

(regime changes, “export of democracy”)

• In software creation (software design)

Some types of design are governed by fundamental laws

(mechanics, electricity, material physics and chemistry), others are

subject to rules which humans (may think to) create. The latter

makes architecture and software design close.

Page 5: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.5

Why architecture is different?

Faced with enormous challenges, software developers wanted to

make the software design process more reliable and well defined,

more ‘scientific’, like in other design activities, “. . . , like X, where Xis any design-intensive profession with a longer and apparentlymore successful history than software” (Doug Lea). Most designs

“with a longer and more successful history” are well (sort of)

founded because they are rooted in the laws and properties of the

natural world.

The architectural design (understood in a broad sense as design

of environment for human existence — buildings, urban area and

artificial landscape) on the other hand has been always

considered both a methodical process and an art form.

Page 6: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.6

Design so beautiful

“I call architecture frozen music”

(Johann Wolfgang Goethe)

Page 7: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.7

Architecture and software

Introduction of the object paradigm in 1980s created big

challenges for its effective use. Original focus “find-the-object”

without due emphasis on interaction and relationship between

classes and objects was not enough to capture “rich structures of

our increasingly complex systems”.

In 1987, Ward Cunningham (CRC cards, Wiki) and Kent Beck

(CRC too, Extreme Programming, JUnit) introduced the ideas of

design patterns to create Smalltalk programming environment.

The idea of design patterns was introduced not by a software

developer, but by an architect, Christopher Alexander. When he

started the professional career (early 1960s), architects began

using computerized tools and computer-like algorithms in building

design of modular construction. Alexander soon convinced himself

that this approach was threatening the architectural quality —

beauty, elegance, harmony were disappearing, and living

environment got dehumanized.

Page 8: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.8

Christopher Alexander

born in Austria

educated in UK

lives and works in USA

professor emeritus of architecture

(in Berkeley, California)

Ch. Alexander is not an ordinary architect. Early in his life he was

a mathematician: “played with operations research, linearprogramming, all the fascinating toys, which mathematics andscience have to offer us, and tried to see how these things cangive us a new view of design, what to design, and how to design”.He later “realized that this view is essentially not productive, andthat for mathematical and scientific reasons, if you like, it wasessential to find a theory in which value and fact are one, in whichwe recognize that here is a central value, approachable throughfeeling, and approachable by loss of self, which is deeplyconnected to facts, and forms a single indivisible world picture”(from The Timeless Way of Building).

Page 9: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.9

Alexander and Software Engineering

His books are (or, at least, used to be) more popular among

software designers than among fellow architects:

• A Pattern Language, 1977 (contains 253 patterns — thank

goodness we are not studying architecture!)

• The Timeless Way of Building, 1979

• The Nature of Order, 4 books 199x

Wiki claims that his influence extends much further then OO

Design Patterns: back in 1960s, another his book Notes on theSynthesis of Form was a required reading for researchers in

computer science (in MIT, at least). And that the very Wiki (Web

2.0) was invented by Ward Cunningham as a medium for hosting

The Hillside Group — the internet-based community to promote

the Design Pattern philosophy.

One of the most successful computer game creators, Will Wright,

ascribes the origin of the Sims games to Alexander’s influence.

Page 10: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.10

Patterns of great architecture

According to Alexander, the great architectures:

• not made from rigorous, planned designs

• made of pieces custom fit to each-other and to their

environment

• aesthetics is attuned to human needs and comfort

• all involve recurring themes (called him patterns)

Patterns helped Alexander to “express design in terms of

relationship between parts of a house and the rules that transform

these relationships.” This is a holistic approach compared to a

method of building houses from predesigned modules.

Page 11: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.11

Examples of architectural patterns

Window on Two Sides of Every RoomThis pattern does not prescribe the size of the windows, or the

distance between them, their height from the floor, what frames

etc. The improved quality comes from the incontestable fact that a

room with two windows is better then a room with a single window.

A Place to WaitDedicated waiting (in doctor’s reception, in an airport etc) is not a

enjoyable experience (unpredictability). Solution: create a

situation which makes waiting positive — fuse in other activities

(magazines, coffee, etc), something which draws people in, not

just waiting, or, the opposite, make waiting place to draw people

into reverie, quiet, positive silence.

Alexander’s patterns have two components: utilitarian and

aesthetic. Some of the pattern names are quite poetic (Wings ofLight, Sleeping in Public), other are kind of philosophical (Zen View).

Page 12: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.12

Parts and the Whole

From The Timeless Way of Building:

• Design is often thought of as a process of synthesis, a

process of putting together things, a process of combination.

• A whole is created by putting together parts. The parts come

first: and the form of the whole comes second.

• But it is impossible to form anything which has the character

of nature by adding preformed parts.

• When parts are modular and made before the whole, by

definition then, they are identical, and it is impossible for

every part to be unique, according to its position in the whole.

Patterns emphasize the rôle of relationship between the parts in

determining the nature of the whole.

Page 13: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.13

What is Design Pattern?

According to Alexander:

“A piece of literature that describes a design problem and a generalsolution for the problem in a particular context”“Each is a three-part rule, which expresses a relation between acertain context, a problem and a solution. The pattern is . . . at the

same time a thing, which happens to the world, and the rule which

tells us how to create that thing, and when we must create it. It is

both a process and a thing; both a description of a thing which is

alive and a description of the process which will generate that

thing” (The Timeless Way of Building).

A pattern is a conflict, its resolution and the resulting synthesis.

Page 14: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.14

Alexandrian pattern template

IF

• you find yourself in { context }• for example { examples }• with { problem }• entailing { forces }

THEN

• for some { reasons }• apply { design from } and/or { rule }• to construct { solution }• leading to { new context } and { other paterns }

Page 15: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.15

Pattern formal structure

• Name

• Intent

• Problem

• Context

• Forces (Participants and Collaborators)

• Solution

• Sketch (Implementation)

• Resulting context (incl. related patterns)

Page 16: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.16

Alcove example

179. Alcoves. . . many large rooms are not complete unless they have smaller rooms andalcoves opening off them. . .

vvv

No homogeneous room, of homogeneous height, can serve a group of peoplewell. To give a group a chance to be together, as a group, a room must also givethem the chance to be alone, in one’s and two’s in the same space.This problem is felt most acutely in the common rooms of a house — thekitchen, the family room, the living room. In fact, it is so critical there, that thehouse can drive the family apart when it remains unsolved. . . In modern life,the main function of a family is emotional; it is a source of security and love.But these qualities will only come into existence if the members of the house arephysically able to be together as a family.This is often difficult. The various members of the family come and go atdifferent times of day; even when they are in the house, each has his ownprivate interests. . . In many houses, these interests force people to go off to theirown rooms, away from the family. This happens for two reasons. First, in anormal family room, one person can easily be disturbed by what the others aredoing. . . Second, the family room does not usually have any space where peoplecan leave things and not have them disturbed. . .

Page 17: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.17

Alcove example (concl.)

To solve the problem, there must be some way in which the members of thefamily can be together, even when they are doing different things.Therefore:Make small places at the edge of any common room, usually no more than 6 feetwide and 3 to 6 feet deep and possibly much smaller. These alcoves should belarge enough for two people to sit, chat, or play and sometimes large enough tocontain a desk or table.

vvv

Give the alcove a ceiling which is markedly lower than the ceiling height in themain room. . .

Page 18: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.18

The Wheel design pattern

Patterns are about re-use of design ideas: Don’t re-invent the

Wheel• Intent Need to move heavy objects over long distances efficiently

without use of machinery

• Problem Heavy things are hard or impossible to carry

• Context But we need to move lots of stuff around now that we have

people living in towns who don’t grow their own food, trade is really

important. . .

• Forces Wheel, axle, bearings, propulsion system (person or beast),

road

• Solution Build vehicles with wheels and smooth surfaces (roads) for

them to roll

• Resulting context Greatly increased capacity and speed. One

person or horse can move much more stuff by pulling a cart than by

carrying it. In the longer term, people who control roads get a lot of

power. In the really long term, more and more roads get built until

the entire planet is covered in concrete. . .

• Sketch Uh. . . shall I explain? (whoever did it was a genius, though)

• Alternatives Carry the stuff on the backs of people, or on the backs

of horses or other beasts of burden; drag it along the ground; put it

on a boat or barge

Page 19: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.19

Patterns in Software

Types of Software Patterns (vis-á-vis Software Life Cycle)

• Analysis Patterns

• Design Patterns

• Implementation Patterns

• Process Patterns

• Project Planning Patterns

• Configuration Management Patterns

We will mostly discuss Design Patterns

Page 20: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.20

Abstraction Levels in Software Patterns

• Architectural Patterns (aka macro-architectures)These are templates for concrete software architectures —provide central organising concept which determines thesystem integrity. Examples:

– Dataflow batch processing, pipes and filters, process control

(polling, callbacks, event-driven)

– Independent component comm. processes, distributed

(client-server, P2P — different topologies: star, ring,

small-world network), agents, MVC (though, it rather can be

thought as a synthetic architecture)

– Call-and-Return main and subroutines, OO systems,

hierarchical layers (eg OS, network applications)

– Virtual machines interpreters, rule-based systems

– Repositories databases, blackboards, search engines

In large systems, an architecture usually involves a

combination of several architectural patterns (eg,

Client-Server in a distributed network with the star topology)

• Design Patterns proper (aka micro-architectures)

• Idioms (aka nano-architectures) They deal with the

implementation of particular design issues

Page 21: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.21

Idioms

Idioms are low-level patterns specific to a particular programming

language. Examples of idioms are typically found at the

programming language level. According to James Coplien: “You

can master a language only after its idioms become second

nature” (IEEE Software, 14(1), p. 36–42, 1997)

In C programming, when a beginner would code like this:

int plaincmp(char* s1, char* s2) {int i;for (i = 0; s1[i] == s2[i]; i++)

if (s1[i] == ’\0’)return 0;

return s1[i] � s2[i];

}

the experienced programmer simply writes:

int idiomcmp(char* s1, char* s2) {//pointer arithmetic and "statement is expression" are crucial

while (*s1++ == *s2++)if (*s1 == ’\0’)

return 0;return *s1 � *s2;

}

Page 22: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.22

Idioms and Patterns

Constructs which are application of DPs in one language can be

idioms in another language. According to J. Gil & D. H. Lorenz:

“. . . one person’s pattern can be another person’s primitive. . . [they

are] cover-ups for shortcomings of a language. The GoF choice of

object-oriented languages to express their design ideas mean that

they did not have to describe things which are inbuilt in those

languages, while being possible to implement in others. If we

assumed procedural languages [as opposed to OO languages],

we might have included patterns called inheritance, encapsulation,

and polymorphism.” (Do you know how to implement in C:

inheritance or polymorphism, albeit in a simplified form?)

“The history of C++ is that it was a preprocessor built over the

procedural, non-OO language C; many OO ideas were originally

developed in Simlua 67 before getting a clearer expression in

Smalltalk. Simula 67 is a derivative of Simula 1, a simulation

package which developed as a wrapper over the very simple

language Algol 60 (which itself had no data structures apart from

arrays).” (“Design Patterns and Language Design”, IEEEComputer, March 1998, p. 118–120)

Page 23: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.23

Patterns?= idioms

Other idioms come with languages features or libraries for

everyday “tricky” tasks such as reading lines of text from standard

input until the end of file. The same function can be implemented

differently depending on the exception support by the language:

With Exceptions

loopread next itemprocess item

end loopexception-handler:

when EOF => continuewhen others =>propagate error-exception

Without Exceptions

loopread next itemif read-past-EOF then

break-exit loopelse process item

end loop

in Javatry {

for (;;) {process(stream.next());

}catch (StreamEndException e) {

stream.close();}

in Cwhile ((x=getchar()) != EOF)

process(x);

OR even like this

while (g = scanf("%d", &n))process(n);

Languages (like C) can “ameliorate” for the lack of exception support by treating a statement

as an expression. (Eiffel does provides some exception support on the language level through

rescue-retry keywords and through design-by-contract features.)

Page 24: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.24

Patterns⇒ Features

Singleton in Eiffel and Java:

in Javapublic class Singleton {

private static Singleton inst = null;private Singleton() { ... }

public static Singleton Instance() {if (inst == null)

inst = new Singleton();return inst;

}}

in Eiffelclass Singleton {ANY}

creation make_singletonfeature

make_singleton isonce

...end

end

single: Singletoncreate single.make_singleton

Other examples:

• clone in Java — a rather controversial AIP-based idiom, and

(again) a language feature in Eiffel• direct encapsulation in Java (private access modifier) and

(rather concocted) export to NONE in Eiffel (with subsequent

subclassing (needs two objects for each one parent, to

implement and enforce the equivalent of Java’s private)

Page 25: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.25

Patterns⇒ Features, Idioms⇒ Features

The language may change and idiom become a feature:

• Type-safe enumeration classes in pre-5 Java which became unnecessary after making

enums the language feature

• Prototype and Memento patterns are trivial idioms in Eiffel — but are fully discussed as

design patterns in GoF

• Multiple dispatch: “one early idiom shows programmers the right way to fake multiple

dispatch in C++” (Coplien 1997)

• Reference counting and other actions associated with reading or writing pointers in OO

languages (this amounts to "putting back" a feature that has become hidden from the

programmer in the advnaced language design).

• Encapsulating code fragments (eg, function’s body) into an object is a language idiom

(not DP since every language does it differently) which may become a language feature

if closures are accepted in Java 7

Why should designers care about idioms? If the designer

intends to use some particular language feature heavily

(exception handlers, for example) then this either constrains

the choice of implementation language or may increase the

complexity of the implementors’ job (to simulate exceptions)

and increase the risks of errors.

Page 26: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.26

Design Patterns in SE

Why patterns became so important for OO design?

OO technology was introduced to cope with rising complexity of

software. The previous major paradigm, the structural

programming, had limited abstraction. But abstraction implies

complexity. The OO complexities manifest themselves in the

relationships between:

• procedure and associated data (encapsulation)

• procedure names to multiple procedures (polymorphism)

• classes to their parent and sibling classes (inheritance)

Patterns provide the vocabulary to grasp these complexities, they

extend beyond objects and tie them together.

In mid 1990s, at the peak of DP popularity, there was a hope of

creating Pattern Languages of Programming (or, PLOP). The idea

was to define pattern semantics, similar to how class and objectare defined in OO languages. Unsurprisingly, these hopes did not

live up to expectation (plop!).

Page 27: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.27

Design Patterns benefits

The design patterns benefits for software developers:

1 Design patterns make it easier to reuse successful designs

and architectures.

2 Expressing proven techniques as design patterns makes

them more accessible to developers of new systems.

3 Design patterns help you choose design alternatives that

make a system reusable and avoid alternatives that

compromise reusability.

4 Design patterns can even improve the documentation and

maintenance of existing systems by furnishing an explicit

specification of class and object interactions and their

underlying intent.

Put simply, design patterns help a designer get a design “right”

faster.

Page 28: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.28

Software pattern template

In software, the pattern template is somewhat different to

Alexandrian pattern template:

Item Description

Name (incl. alias) A unique identifier

Intent The pattern purpose

Problem The problem to be solved

Solution Description of solution

Participant and The entities (usually classes)

Collaborators involved in the pattern

Consequences The pattern trade-offs;

discussion of the forces at play

Implementation How to implement it

GoF Reference Page # in the GoF book

Page 29: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.29

Misconceptions about what patterns are

This discussion of pattern misconception follows the book “Pattern

Hatching” by (late) John Vlissides.

Take the most commonly used definition:

“A pattern is a solution to a problem in a context.”Counterexample:

• Problem I won a lottery, but my dog has eaten the ticket.

• Context There is deadline on claiming the prize (in USA,

apparently, not sure about Australia). It is one hour from now.

• Solution Cut the dog open, get the ticket out, run to the News

Agent. (sorry RSCPA!)

Why this is absurd?

Missing principles:

• Recurrence Is the solution relevant elsewhere?

• Teaching Can the solution be adopted in a different context?

• Name Can you put a name on the “pattern”?

Page 30: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.30

All aspects of the pattern definition

• Name Generation Gap

• Context After a code was generated by a code-generation

tool (eg, in Netbeans GUI builder), a programmer modifies

the code altering the behaviour.

• Problem How do you prevent subsequent regeneration from

corrupting a programmer’s modifications?

• Forces

1 need to control the access to generated internals

2 comments-based approach ugly, unchecked

3 auto-diff-and-merge too imprecise

• Solution Use inheritance to separate generated code from

modifications (generated classes are immutable)

• Consequences

(+) modifications are decoupled from generated code

(+) modifications can have privileged access

(+) subsequent regeneration doesn’t mean reapplying changes

(–) double the number of classes

(–) complicates integration into existing class hierarchies

Page 31: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.31

Misconceptions about what patterns can do

Patterns are just are

• Jargon? Not just jargon: a good pattern writer avoids

patter-specific jargon

• Rules? Patterns are not meant to be applied automatically

• Programming tricks or data structures? The solution is an

important aspect, but there are equally important others –

problem, context, intent. . .

Misconceptions about pattern use

You need tools or methodological support to be effective: No!

Patterns are primarily food for the brain.Main benefits (again):

• Capturing expertise

• Facilitate communication

• Provide effective documentation

• Allow more efficient design and redesign (restructuring)

Page 32: Design Patterns I - Introduction: Christopher Alexander's ...courses.cecs.anu.edu.au/courses/archive/comp2110.2008/lectures/lec... · Christopher Alexander Great architecture What

Design Patterns • I

Alexei Khorev

Design and patterns

Designs humans do

Architectural design

Christopher Alexander

Great architecture

What is DP?

Patterns in Software

Patterns and Idioms

DPs for Software

DP Misconceptions

15.32

Patterns and world’s peace

“Patterns do not guarantee reusable software, higher productivity,

world peace, etc. Patterns do nothing to remove the human from

the creative process. Patterns are just another weapon in the

developer’s arsenal. To ascribe more is counterproductive.

Under-promise and over-deliver — that’s the best defense against

hype and backlash.”

Patterns generate whole architecture

“Generative aspect of patterns means the ability to create

emergent behaviour: helps readers solve problems that the

pattern does not address specifically.”

Patterns are for OO design or implementation

Plainly, not true! The GoF indeed covers general OO

programming, but a large and growing body of patterns specific to

architecture, process and organization, distributed and concurrent

programming, event-driven programming etc is documented in the

volumes of Pattern Languages of Program Design (4 volumes)

and elsewhere.