33
Introduction to Patterns Hironori Washizaki Waseda University Twitter: @Hiro_Washi [email protected] http://www.washi.cs.waseda.ac.jp/

Introduction to Patterns (miniPLoP@Taipei)

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Introduction to Patterns (miniPLoP@Taipei)

Introduction to Patterns

Hironori WashizakiWaseda University

Twitter: @Hiro_Washi [email protected]://www.washi.cs.waseda.ac.jp/

Page 2: Introduction to Patterns (miniPLoP@Taipei)

Agenda

• Patterns and Pattern Languages• Software Patterns• Writing Patterns

2

Page 3: Introduction to Patterns (miniPLoP@Taipei)

PATTERNS AND PATTERN LANGUAGES

3

Page 4: Introduction to Patterns (miniPLoP@Taipei)

4

Stockholm, Sweden

Alaior, Spain

Page 5: Introduction to Patterns (miniPLoP@Taipei)

Repetition, and, not a coincidence.

• Small public squares

• Street cafes

• Live! Active! Positive!

5

Page 6: Introduction to Patterns (miniPLoP@Taipei)

What makes this repetition?

6

• Settings were common– Planning city structure and environment

• Problems were common– Open and attractive city– Having places for people gathering and sitting lazily

• Considerations were common– Not too large space– Not closed.

=> Common solution!Solution

Problem

Context

Forces

Page 7: Introduction to Patterns (miniPLoP@Taipei)

Pattern form• Context: when to consider?

• Problem: what should be solved and when?

• Forces: why the problem is hard?

• Solution: what to do to solve problem?

• Resulting context: both positive and negative 7

Solution

Problem

Context

Forces

Adapted from Joseph Yoder, "AsianPLoP Pattern Bootcamp 2011"

Page 8: Introduction to Patterns (miniPLoP@Taipei)

SMALL PUBLIC SQUARES

• … this pattern forms the core which makes an ACTIVITY NODE … it can also help to generate a PROMENADE, …, through the action of the people who gather there…

• A town needs public squares; they are the largest, most public rooms, that the town has. But when they are too large, they look and feel deserted.

• Make a public square much smaller than you would at first imagine…

8

Solution

Problem

Context

Forces

Christopher Alexander, et al., “A Pattern Language,“ Oxford University Press, 1977

Page 9: Introduction to Patterns (miniPLoP@Taipei)

Abstraction and concretization

9

AbstractionConcretization

Page 10: Introduction to Patterns (miniPLoP@Taipei)

Alexander’s definition of patterns

• Describes a problem that occurs over and over again in our environment

• Describes the core of the solution to that problem

• In such a way that you can use this solution a million times over without ever doing it the same way twice.

• 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

10

Christopher Alexander, et al., “A Pattern Language,“ Oxford University Press, 1977Christopher Alexander , “The Timeless Way of Building,” Oxford University Press, 1979

Adapted from Joseph Yoder, "AsianPLoP Pattern Bootcamp 2011"

Page 11: Introduction to Patterns (miniPLoP@Taipei)

Quality Without A Name (QWAN)

• “There is a central quality which is the root criterion of life and spirit in a man, a town, a building, or a wilderness. This quality is objective and precise, but it cannot be named.”– Christopher Alexander

• Message from C. Alexander

11Adapted from Joseph Yoder, "AsianPLoP Pattern Bootcamp 2011"

Page 12: Introduction to Patterns (miniPLoP@Taipei)

Thinking and communications by patterns

… OK, so, to attract many people to our city, SMALL PUBLIC SQUAREs should be located in the center. At the SMALL PUBLIC SQUARE, make STREET CAFES be OPNENING TO THE STREET...

12

BuildingStreet

Public square

Cafe

Page 13: Introduction to Patterns (miniPLoP@Taipei)

Pattern Language• “A collection of patterns and

the rules to combine them into an architectural style.”– James O. Coplien

• “Each pattern then, depends both on the smaller patterns it contains, and on the larger patterns within which it is contained.”– Christopher Alexander

13

SMALL PUBLIC SQUARE

STREET CAFES

OPENING TO THE STREET DIFFERENT

CHAIRS

ACTIVITY NODES

Adapted from Joseph Yoder, "AsianPLoP Pattern Bootcamp 2011"

Page 14: Introduction to Patterns (miniPLoP@Taipei)

SOFTWARE PATTERNS

14

Page 15: Introduction to Patterns (miniPLoP@Taipei)

History of patterns and JapanC. Alexander: A Pattern Language for Building

K. Beck and W. Cunningham: Application of pattern language to software at OOPSLAE. Gamma: Doctoral thesis on object-oriented design patterns

E. Gamma et al.: Design PatternsPLoP conference started

(Many books on patterns)

Japan PLoP started as study meetings

IPSJ SIGSE Patterns WG

1979

1987

1990

1995

1994

2003

AsianPLoP conference started2010

1999

(OOPSLA workshops)

Adapted from Takeshi Inoue, “Introduction to Patterns”, IPSJ SIGSE Patterns WG, 2003

Page 16: Introduction to Patterns (miniPLoP@Taipei)

16

Software pattern

• A pattern is a proven solution to a problem in a software context.

• What software community adopted– Tool for knowledge transfer and communication– Pattern form: context-problem-solution– Pattern catalog (and partially language..)– Especially object-orientation community

• What software community did NOT adopt– Common language among stakeholders

Page 17: Introduction to Patterns (miniPLoP@Taipei)

17What’s the problem?class Mathematic {

public Data sort(Data data){ switch(settings) { case QUICK: return quickSort(data); case BUBBLE: return bubbleSort(data); default: ... }}

class Loan {

public double capital() { if(expiry == null && maturity != null) return ...; if(expiry != null && maturity == null) { ... }

Page 18: Introduction to Patterns (miniPLoP@Taipei)

18class Mathematic {

public Data sort(Data data){ switch(settings) { case QUICK: return quickSort(data); case BUBBLE: return bubbleSort(data); default: ... }}

class Loan {

public double capital() { if(expiry == null && maturity != null) return ...; if(expiry != null && maturity == null) { ... }

class Mathematic { Sorter sorter; public Data sort(Data data){ return sorter.sort(data); }

abstract class Sorter { public abstract Data sort(Data);

class QuickSorter extends Sorter { public Data sort(Data) { ... }

class Loan { CapitalCalc capitalCalc; public double capital(){ return capitalCalc.calc(this); }

Interface CapitalCalc { double calc(Loan l);

class TermCapital implements ...{ double calc(Loan l) { ... }

Page 19: Introduction to Patterns (miniPLoP@Taipei)

19STRATEGY

Context Strategy

algorithmInterface()

ConcreteStrategyA

algorithmInterface()

ConcreteStrategyB

algorithmInterface()

・・・

・・・

contextInterface()

Structure

Motivation If there are hard-wiring line breaking algorithms, clients get bigger and harder to maintain. Moreover it becomes difficult to add new algorithms and vary existing ones…

ApplicabilityMany related classes differ only in their behavior. You need different variants of an algorithm…

ConsequencesBenefits: families of algorithms , elimination of conditional statements…Drawbacks: Communication overhead…

Solution

Problem

Context

Forces

E. Gamma, et al. “Design Patterns: Elements of Reusable Object-Oriented Software,”  Addison-Wesley, 1994.

Page 20: Introduction to Patterns (miniPLoP@Taipei)

20Applying STRATEGY

Mathematic Sorter

sort(Data)

QuickSorter

sort(Data)

BubbleSorter

sort(Data)

・・・

・・・

Client

sort(Data)setSorter(Sorter)

ContextStrategy

ConcreteStrategy

class Client { Mathematic math;

void init() { math.setSorter( new QuickSorter()); } void calc() { data = math.sort(data);}

class Mathematic { Sorter sorter; public Data sort(Data data){ return sorter.sort(data); }

abstract class Sorter { public abstract Data sort(Data);

class QuickSorter extends Sorter { public Data sort(Data) { ... }

Page 21: Introduction to Patterns (miniPLoP@Taipei)

21Benefit of patterns and pattern form

• Reuse– Solution– Problem

• Communication• Understanding• Way of thinking• Generative. New ideas!

Page 22: Introduction to Patterns (miniPLoP@Taipei)

22What’s going on?interface MessageStrategy { public class HelloWorld { public void sendMessage();                 public static void main(String[] args) { } MessageBody mb = new MessageBody();abstract class AbstractStrategyFactory { mb.configure(“Hello World!”); public abstract MessageStrategy createStrategy(MessageBody mb); AbstractStrategyFactory asf

= DefaultFactory.getInstance();class MessageBody { MessageStrategy strategy object payload; = asf.createStrategy(mb); public Object getPayload() { mb.send(strategy); return payload; } } } public void configure(Object obj) { payload obj; } public void send(MessageStrategy ms) { ms.sendMessage(); }} class DefaultFactory extends AbstractStrategyFactory { private DefaultFactory() {} static DefaultFactory instance; public static AbstractStrategyFactory getInstance() { if(instance == null) instance = new DefaultFactory(); return instance; } public MessageStrategy createStrategy(final MessageBody mb) { return new MessageStrategy() { MessageBody body = mb; public void sendMessage() { Object obj = body.getPayload(); System.out.println(obj); }   }; } }

Joshua Kerievsky, "Refactoring to Patterns," Addison-Wesley, 2004.

Page 23: Introduction to Patterns (miniPLoP@Taipei)

23Pitfall of software patterns

• “Only solution is important.”– Context, problem and forces are most important!

• “Should use as it is.”– There could be variants.

• “Always beneficial.”– Misuse leads to bad complexity and defects.

• “Should use at the beginning.”– Simple design at the beginning, and refactor it!

Page 24: Introduction to Patterns (miniPLoP@Taipei)

24E.g. Replace Conditional Logic with STRATEGY

Mathematic Sorter

sort(Data)

QuickSorter

sort(Data)

BubbleSorter

sort(Data)

・・・

・・・

Client

sort(Data)

Replace Conditional with Polymorphism

MathematicClient

sort(Data)

MathematicClient

sort(Data)

Sorter

sort(Data)

Move method

if ...else ...

if ...else ...

Joshua Kerievsky, "Refactoring to Patterns," Addison-Wesley, 2004.

Page 25: Introduction to Patterns (miniPLoP@Taipei)

Pattern catalogs and languages• Product patterns

– “Analysis patterns” (M. Fowler)– “Pattern-Oriented Software Architecture” (Bushmann et al)– “Design Patterns: Elements of Reusable Object-Oriented Software”

(Gamma et al.)– “Implementation patterns” (Beck)– “xUnit Test Patterns” (Meszaros)– “Object-Oriented Reengineering Patterns” (Nierstrasz et al)

• Process and organizational patterns– “EPISODE” (Cunningham)– "A Generative Development-Process Pattern Language” (Coplien)– "Organizational Patterns of Agile Software Development“ (Coplien and

Harrison)• Links to catalogs

– “Pattern almanac” (Linda Rising)– Portland Pattern Repository (Cunningham http://c2.com/ppr/) 25

Page 26: Introduction to Patterns (miniPLoP@Taipei)

Network in Portland Pattern Repository

Pattern name

N. patterns referred by the pattern

N. patterns referring

to the pattern

ModelViewController 11 12AdapterPattern 6 15

HandleBodyPattern 9 10SynchronizationStrategies 9 9

VisitorPattern 7 11SceneGraph 6 11ValueObject 3 14ScapeGoat 6 10

CompositePattern 4 12StrategyPattern 5 11

26

Hironori Washizaki, Masashi Kadoya, Yoshiaki Fukazawa and Takeshi Kawamura, “Network Analysis for Software Patterns including Organizational Patterns in Portland Pattern Repository,” Agile 2014 Conference (to appear)

Page 27: Introduction to Patterns (miniPLoP@Taipei)

27

ENGAGE CUSTOMERS• ...an organization is in place,

and its Quality Assurance function has been generally shaped and chartered…

• It's important that the development organization ensures and maintains customer satisfaction by encouraging communication between customers and key development organization roles…

• Closely couple the Customer role to the Developer and Architect, not just to QA or marketing…

James O. Coplien, Neil B. Harrison, "Organizational Patterns of Agile Software Development", Prentice Hall, 2004.

James O. Coplien, "A Development Process Generative Pattern Language," PLoPD

Page 28: Introduction to Patterns (miniPLoP@Taipei)

From patterns to Agile development

28

Pattern languageTakeuchi The New New Development Game, 1986

Design patternsOO patterns

A Generative Development-Process Pattern Language EPISODE

XPScrum

Kenji Hiranabe: From Software Patterns to Agile Movementshttp://www.infoq.com/jp/articles/AlexanderFestaReport

Page 29: Introduction to Patterns (miniPLoP@Taipei)

WRITING PATTERNS

29

Page 30: Introduction to Patterns (miniPLoP@Taipei)

Light-weight pattern writing

1. Individual: write down good experiments or important things.

2. Team: brainstorming– Grouping, relating– Add, modify

3. Team: write in pattern form from important groups– (1) Write context and resulting context– (2) Write context, problem and solution– (3) Identify forces– (4) Name it!

30

Page 31: Introduction to Patterns (miniPLoP@Taipei)

E.g. Self continuing education

31

I can study while commuting because of no interruption..

I always carry short literature for little vacant time…

Smartphone for free time…

Study group meetings on Fridays…

Wake up early to …

Plan reading groups in my company…

I set concrete goals in 1, 5, 10 years…

CARRYING SHORT LITERATURE

ContextYou want to enrich your knowledge in an additional and unfamiliar area by reading some literatures. ProblemYou are too busy to make time for studying at your home and office. Forces-Making time specific for study will sacrifice your family considerations and business.-There are a number of discrete short times during your commuting…SolutionSelect short literatures and carry them at all times so that you could read them even in short time during commuting. Resulting ContextYou are now enriching your knowledge continuously!

Page 32: Introduction to Patterns (miniPLoP@Taipei)

32

From patterns to pattern languages• Connect related

patterns– X is similar to Y.– X uses Y in its solution.– X can be combined

with Y.• Identify surrounding

patterns

ACCUMULATION OF COMMUTING HOURS

SPLITTING FAT BOOKS

CARRYING SHORT LITERATURE

CARRYING E-BOOK READER

BOOK SCANNING

Page 33: Introduction to Patterns (miniPLoP@Taipei)

33

Summary• Pattern: a proven solution to a problem in a

software context.• Pattern form: context, problem, forces,

solution, resulting context• Pattern Language: individual patterns are

useful, but they are most powerful when combined into a language. (Alexander)

• Benefit and pitfall of patterns• Various software patterns: design patterns and

organizational patterns• Writing your own patterns: easy and fun!