design pattern automation

69
@gfraiteur a fresh view on design patterns, programming languages, and agile methodologies Design Pattern Automation Gaël Fraiteur PostSharp Technologies Founder & Principal Engineer

Upload: postsharp-technologies

Post on 09-Feb-2015

1.319 views

Category:

Technology


3 download

DESCRIPTION

Design patterns form a language, but programming languages have still poor support for design patterns. Gael Fraiteur calls for smarter compilers that would allow to code at the right level of abstraction, with design patterns.

TRANSCRIPT

Page 1: Design Pattern Automation

@gfraiteur

a fresh view on design patterns, programming languages,

and agile methodologies

Design Pattern Automation

Gaël Fraiteur

PostSharp TechnologiesFounder & Principal Engineer

Page 2: Design Pattern Automation

@gfraiteur

Hello!

My name is GAEL.

No, I don’t thinkmy accent is funny.

my twitter

Page 3: Design Pattern Automation

@gfraiteur

My commitment to you:to seed a vision, not to sell a tool.

Page 4: Design Pattern Automation

@gfraiteur

The vision:Code at the right level of abstraction,with compiler-supported design patterns

Page 5: Design Pattern Automation

@gfraiteur

NotifyPropertyChanged,the wrong way

LAB 1

Page 6: Design Pattern Automation

@gfraiteur

1. A history of programming languages and patterns

2. Defining Design Pattern Automation

3. Pattern-Oriented Software Development

4. Tools for Design Pattern Automation

5. Agile Methodologies and Patterns

6. A last example

7. Q&A

8. Summary

Section

Page 7: Design Pattern Automation

@gfraiteur

Univac Assembly Language

Page 8: Design Pattern Automation

@gfraiteur

FORTRAN (1955)• 1955 - FORTRAN I

• Global Variables

• Arrays

• 1958 - FORTRAN II

• Procedural Programming(no recursion)

• Modules

Page 9: Design Pattern Automation

@gfraiteur

COBOL (1959)• Data Structures

• Natural Language

Page 10: Design Pattern Automation

@gfraiteur

LISP (1959)• Variable Scopes (Stack)

• Garbage Collection

Page 11: Design Pattern Automation

@gfraiteur

Simula (1967)• Classes as

abstraction mechanism

• Virtual procedures

• Object references

• Coroutines

Begin Class Glyph; Virtual: Procedure print Is Procedure print; Begin End;

Glyph Class Char (c); Character c; Begin Procedure print; OutChar(c); End;

Glyph Class Line (elements); Ref (Glyph) Array elements; Begin Procedure print; Begin Integer i; For i:= 1 Step 1 Until UpperBound (elements, 1) Do elements (i).print; OutImage; End; End;

Ref (Glyph) rg; Ref (Glyph) Array rgs (1 : 4);

! Main program; rgs (1):- New Char ('A'); rgs (2):- New Char ('b'); rgs (3):- New Char ('b'); rgs (4):- New Char ('a'); rg:- New Line (rgs); rg.print;End;

Page 12: Design Pattern Automation

@gfraiteurCopyright © by Arild Vågen

Human Language

Cognition

Communication

Social Organization

Machine Language

Execution

Programming Languages

Page 13: Design Pattern Automation

@gfraiteurHolašovice, Czech Republic

How is it possible that any simple farmer could make a house, a thousand times more beautiful than all the struggling architects of the last fifty years could do?

Christopher Alexander in The Timeless Way of Building, 1977

Pattern Thinking (1977)

Gael Fraiteur
Page 14: Design Pattern Automation

@gfraiteur

A world of patterns

LANGUAGE

REGULARITYRESOLUTION

OF FORCESSELF-

SUSTAINABILITY

VARIETY UBIQUITY

Page 15: Design Pattern Automation

@gfraiteurLanguage Enables Cognition

Page 16: Design Pattern Automation

@gfraiteurCopyright © Stijn NieuwendijkLanguage Enables Communication

Page 17: Design Pattern Automation

@gfraiteur

Language for the Trade

Page 18: Design Pattern Automation

@gfraiteur

Patterns as

WISDOM in canonical form

• Name

• Also Known As

• Problem

• Context

• Forces

• Example

• Solution

• Structure and Dynamics

• Implementation

• Example Resolved

• Known Uses

• Variants

• Consequences

• Relationships

• Name

• Also Known As

• Problem

• Context

• Forces

• Example

• Solution

• Structure and Dynamics

• Implementation

• Example Resolved

• Known Uses

• Variants

• Consequences

• Relationships

• Name

• Also Known As

• Problem

• Context

• Forces

• Example

• Solution

• Structure and Dynamics

• Implementation

• Example Resolved

• Known Uses

• Variants

• Consequences

• Relationships

• Name

• Also Known As

• Problem

• Context

• Forces

• Example

• Solution

• Description

• Example Resolved

• Known Uses

• Variants

• Consequences

• Relationships

Page 19: Design Pattern Automation

@gfraiteur

Software Patterns (1995)• Apply to OO design

• Canonic pattern description

• First catalogue of 23 patterns

Page 20: Design Pattern Automation

@gfraiteur

In 2013, eighteen years later, design patterns still live in their parent’s house – books.

Page 21: Design Pattern Automation

@gfraiteur

Latent Redundancy

Bad OO code Good OO code Ideal code

Intrinsic variability Latent redundancy Bad redundancy

Page 22: Design Pattern Automation

@gfraiteur

Cost of low abstraction• Boilerplate code

• More bugs

• Code review

• Changing the pattern implementation

Page 23: Design Pattern Automation

@gfraiteur

1. A history of programming languages and patterns

2. Defining Design Pattern Automation

3. Pattern-Oriented Software Development

4. Tools for Design Pattern Automation

5. Agile Methodologies and Patterns

6. A last example

7. Q&A

8. Summary

Section

Page 24: Design Pattern Automation

@gfraiteur

Pattern Automation

Automation is the use of machines, control systems and information technologies to optimize productivity in the production of goods and delivery of services.

Wikipedia

Design Pattern Automation is the use of tools to optimize productivity in the implementation of software based on patterns.

Page 25: Design Pattern Automation

@gfraiteur

Pattern-Aware Compilers allow for coding at the level of abstraction of design patterns.

Page 26: Design Pattern Automation

@gfraiteur

Design Pattern Automation is new

© hellraiser

MYTH 1

Page 27: Design Pattern Automation

@gfraiteur

Pattern C# Keyword

Dispose using

Lock lock

Publish-Subscribe event

C# implements several patternsTRUTH 1

Page 28: Design Pattern Automation

@gfraiteur

But C# does not let you implement your own patterns.

Page 29: Design Pattern Automation

@gfraiteur

code generators were proved wrong

You can’t automate patterns because…

MYTH 2

Page 30: Design Pattern Automation

@gfraiteur

pattern-aware programming languages and compilers

we don’t need code generators but

TRUTH 2

Page 31: Design Pattern Automation

@gfraiteur

there are too many skills involved

You can’t automate patterns because…

MYTH 3

Page 32: Design Pattern Automation

@gfraiteur

industrial revolutionSkill scarcity motivated the

TRUTH 3

Page 33: Design Pattern Automation

@gfraiteur

Our own industrial revolution:

Making it easier to build good software

Page 34: Design Pattern Automation

@gfraiteur

1. A history of programming languages and patterns

2. Defining Design Pattern Automation

3. Pattern-Oriented Software Development

4. Tools for Design Pattern Automation

5. Agile Methodologies and Patterns

6. A last example

7. Q&A

8. Summary

Section

Page 35: Design Pattern Automation

@gfraiteur

they are way too complex general abstract

You can’t automate patterns because…

MYTH 4

Page 36: Design Pattern Automation

@gfraiteur

a normal part of any engineering process

Making abstract things concrete is

TRUTH 4

ConcreteAbstract

Automatable

Non-Automatable

Page 37: Design Pattern Automation

@gfraiteur

Pattern-Oriented Implementation Process

Architecture Pattern

• Defines Subsystems

Design Pattern

• Define Components

• General problem• Language-

independent

Implementation Pattern

• Defines source artefacts

• Language-specific

• Framework-specific

• Application-specific

• Implementation guidelines

Source code

• Executable

High Abstraction Zero Abstraction

Page 38: Design Pattern Automation

@gfraiteur

Example: Observer Pattern

1. Vanilla Design Pattern (GoF)

Page 39: Design Pattern Automation

@gfraiteur

Example: Observer Pattern

1

2

3Call OnPropertyChanged whenever a property has changed.

2. Language-Specific Pattern (C#)

Page 40: Design Pattern Automation

@gfraiteur

• Analyze property-field dependencies

• Modify all non-const methods

• Whenever a relevant field has changed

• When invariants are valid (after all changes in the object have been done)

Example: Observer Pattern

When? How?

3. Application-Specific Pattern (C#)

Page 41: Design Pattern Automation

@gfraiteur

Example: Observer Pattern4. Source code

Page 42: Design Pattern Automation

@gfraiteur

NotifyPropertyChangedwith PostSharp

LAB 2

Page 43: Design Pattern Automation

@gfraiteur

1. A history of programming languages and patterns

2. Defining Design Pattern Automation

3. Pattern-Oriented Software Development

4. Tools for Design Pattern Automation

5. Agile Methodologies and Patterns

6. A last example

7. Q&A

8. Summary

Section

Page 44: Design Pattern Automation

@gfraiteur

Transformation

Verification

Page 45: Design Pattern Automation

@gfraiteur

Pattern Automation Technologies

Pattern Automati

on

Page 46: Design Pattern Automation

@gfraiteur

Example:

Richman’s Singleton• Static validation rules:

• A single constructor

• Constructor never called

• Code transformation:

• Introduce static field

• Introduce static method GetInstance

• Make constructor private

Page 47: Design Pattern Automation

@gfraiteur

[Nemerle.MacroUsage (Nemerle.MacroPhase.BeforeInheritance, Nemerle.MacroTargets.Class)] macro Singleton (t : TypeBuilder) { def mems = t.GetParsedMembers();

// find constructor, which we will need to call to create instance def ctor = mems.Filter(fun (x) { | <[ decl: ..$_ this (..$_) $_ ]> => true | _ => false });

match (ctor) { | [ <[ decl: ..$_ this (..$parms) $_ ]> as constructor ] => {

// we must prepare expressions for invoking constructor def invoke_parms = parms.Map(x => <[ $(x.ParsedName : name) ]>); // first define the field, where a single instance will be stored t.Define(<[ decl: private static mutable instance : $(t.ParsedName : name); ]>);

 

Page 48: Design Pattern Automation

@gfraiteur

// finally, define getter t.Define (<[ decl: public static Instance : $(t.ParsedName : name) { get { // lazy initialization in generated code when (instance == null) instance = $(t.ParsedName : name) (..$invoke_parms); instance; } } ]>);

// make sure constructor is protected constructor.Attributes |= NemerleAttributes.Protected; | _ => Message.Error ("Singleton design pattern requires exactly one constructor defined") } }

Page 49: Design Pattern Automation

@gfraiteur

[Singleton]class LoadBalancer{ // Details omitted}

public class SingletonApp{ public static Main() : void { def b1 = LoadBalancer.Instance; def b2 = LoadBalancer.Instance;  // Same instance? when ((b1 : object == b2) && (b2 : object == b3) && (b3 : object == b4)) Console.WriteLine( "Same instance" );  // Do the load balancing Console.WriteLine( b1.Server ); Console.WriteLine( b2.Server ); }}

Page 50: Design Pattern Automation

@gfraiteur

Macro-enabled .NET languages• Nemerle

• Scala

• Boo

Page 51: Design Pattern Automation

@gfraiteur

Poorman’s Pattern

Automation: Static Analysis

Pattern Automati

on

Page 52: Design Pattern Automation

@gfraiteur

Example:

Poorman’s Singleton• Static validation rules:

• Static private readonly field named ‘singleton’, whose type is the current type.

• Static public method GetInstance() whose return type is the current type, returns field ‘singleton’.

• Class constructors are private.

Page 53: Design Pattern Automation

@gfraiteur

Singleton with Architecture Unit Tests

LAB 3

Page 54: Design Pattern Automation

@gfraiteur

• Limitations

• Inadequate UI and error reporting

• Limited reflection API

• Alternatives

• FxCop

• PostSharp Architecture Framework

Architecture Unit Tests

Page 55: Design Pattern Automation

@gfraiteur

1. A history of programming languages and patterns

2. Defining Design Pattern Automation

3. Pattern-Oriented Software Development

4. Tools for Design Pattern Automation

5. Agile Methodologiesand Patterns

6. A last example

7. Q&A

8. Summary

Section

Page 56: Design Pattern Automation

@gfraiteur

All design patterns are found in books

MYTH 5

Page 57: Design Pattern Automation

@gfraiteur

spinning into

Emergent Design

Page 58: Design Pattern Automation

@gfraiteur

Itera

tion 1

Itera

tion 2

Itera

tion 3

Itera

tion 4

Itera

tion 5

Itera

tion 6

Itera

tion 7

Itera

tion 8

Itera

tion 9

Itera

tion 10

Qualit

y

Eroding Design

Quality

Distance from design

Page 59: Design Pattern Automation

@gfraiteur

Itera

tion 1

Itera

tion 2

Itera

tion 3

Itera

tion 4

Itera

tion 5

Itera

tion 6

Itera

tion 7

Itera

tion 8

Itera

tion 9

Itera

tion 10

Qualit

y

Emergent Design

Quality

Knowledge

Page 60: Design Pattern Automation

@gfraiteur

Iterative Design

• Unit Tests• Load Tests• Integration

Tests

• Project• Team• Design

• Abstract• Design• Refactor• Implement

• Time• User Stories

Planning Development

TestingReview

Page 61: Design Pattern Automation

@gfraiteur

Agile & Continuous Improvement

Design

RefactorImplement new stuff

Learn

KnowledgePatternsLanguage

Requirements

Code

Page 62: Design Pattern Automation

@gfraiteur

We’re a part of a whole

Software EngineeringDiscipline

Company / Personal

Know-How

Project

EngineeringDiscipline

Page 63: Design Pattern Automation

@gfraiteurHolašovice, Czech Republic

Patterns emerge naturally from iterative design processes.

TRUTH 5

Gael Fraiteur
Page 64: Design Pattern Automation

@gfraiteur

Example of application-specific pattern:

Business Rule Pattern

• For any class derived from BusinessRule:

• Name ends with “BusinessRule”

• No public constructor

• Has nested class named “Factory”

• Has parameterless public constructor

• Annotated with [Export(typeof(IBusinessRuleFactory))]

Page 65: Design Pattern Automation

@gfraiteur

1. A history of programming languages and patterns

2. Defining Design Pattern Automation

3. Pattern-Oriented Software Development

4. Tools for Design Pattern Automation

5. Agile Methodologies and Patterns

6. A last example7. Q&A

8. Summary

Section

Page 66: Design Pattern Automation

@gfraiteur

Example:

Reader-Writer Synchronized Object

• Code Generation

• Introduce interface IReaderWriterSynchronized

• [ReaderLock], [WriterLock] executes the method with the right locking level

• Code Verification

• All fields must be private

• Cannot access field without proper lock

Page 67: Design Pattern Automation

@gfraiteur

ReaderWriterSynchronized with PostSharp

LAB 4

Page 68: Design Pattern Automation

@gfraiteur

Q&AGael [email protected]@gfraiteur

Page 69: Design Pattern Automation

@gfraiteurCopyright © by Arild Vågen

Summary• Compilers must do more to raise abstraction

level

• Patterns stem from continuous improvement

BETTER SOFTWARE THROUGH SIMPLER CODE