design pattern automation
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
@gfraiteur
a fresh view on design patterns, programming languages,
and agile methodologies
Design Pattern Automation
Gaël Fraiteur
PostSharp TechnologiesFounder & Principal Engineer
@gfraiteur
Hello!
My name is GAEL.
No, I don’t thinkmy accent is funny.
my twitter
@gfraiteur
My commitment to you:to seed a vision, not to sell a tool.
@gfraiteur
The vision:Code at the right level of abstraction,with compiler-supported design patterns
@gfraiteur
NotifyPropertyChanged,the wrong way
LAB 1
@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
@gfraiteur
Univac Assembly Language
@gfraiteur
FORTRAN (1955)• 1955 - FORTRAN I
• Global Variables
• Arrays
• 1958 - FORTRAN II
• Procedural Programming(no recursion)
• Modules
@gfraiteur
COBOL (1959)• Data Structures
• Natural Language
@gfraiteur
LISP (1959)• Variable Scopes (Stack)
• Garbage Collection
@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;
@gfraiteurCopyright © by Arild Vågen
Human Language
Cognition
Communication
Social Organization
Machine Language
Execution
Programming Languages
@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)
@gfraiteur
A world of patterns
LANGUAGE
REGULARITYRESOLUTION
OF FORCESSELF-
SUSTAINABILITY
VARIETY UBIQUITY
@gfraiteurLanguage Enables Cognition
@gfraiteurCopyright © Stijn NieuwendijkLanguage Enables Communication
@gfraiteur
Language for the Trade
@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
@gfraiteur
Software Patterns (1995)• Apply to OO design
• Canonic pattern description
• First catalogue of 23 patterns
@gfraiteur
In 2013, eighteen years later, design patterns still live in their parent’s house – books.
@gfraiteur
Latent Redundancy
Bad OO code Good OO code Ideal code
Intrinsic variability Latent redundancy Bad redundancy
@gfraiteur
Cost of low abstraction• Boilerplate code
• More bugs
• Code review
• Changing the pattern implementation
@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
@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.
@gfraiteur
Pattern-Aware Compilers allow for coding at the level of abstraction of design patterns.
@gfraiteur
Design Pattern Automation is new
© hellraiser
MYTH 1
@gfraiteur
Pattern C# Keyword
Dispose using
Lock lock
Publish-Subscribe event
C# implements several patternsTRUTH 1
@gfraiteur
But C# does not let you implement your own patterns.
@gfraiteur
code generators were proved wrong
You can’t automate patterns because…
MYTH 2
@gfraiteur
pattern-aware programming languages and compilers
we don’t need code generators but
TRUTH 2
@gfraiteur
there are too many skills involved
You can’t automate patterns because…
MYTH 3
@gfraiteur
industrial revolutionSkill scarcity motivated the
TRUTH 3
@gfraiteur
Our own industrial revolution:
Making it easier to build good software
@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
@gfraiteur
they are way too complex general abstract
You can’t automate patterns because…
MYTH 4
@gfraiteur
a normal part of any engineering process
Making abstract things concrete is
TRUTH 4
ConcreteAbstract
Automatable
Non-Automatable
@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
@gfraiteur
Example: Observer Pattern
1. Vanilla Design Pattern (GoF)
@gfraiteur
Example: Observer Pattern
1
2
3Call OnPropertyChanged whenever a property has changed.
2. Language-Specific Pattern (C#)
@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#)
@gfraiteur
Example: Observer Pattern4. Source code
@gfraiteur
NotifyPropertyChangedwith PostSharp
LAB 2
@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
@gfraiteur
Transformation
Verification
@gfraiteur
Pattern Automation Technologies
Pattern Automati
on
@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
@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); ]>);
@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") } }
@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 ); }}
@gfraiteur
Macro-enabled .NET languages• Nemerle
• Scala
• Boo
@gfraiteur
Poorman’s Pattern
Automation: Static Analysis
Pattern Automati
on
@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.
@gfraiteur
Singleton with Architecture Unit Tests
LAB 3
@gfraiteur
• Limitations
• Inadequate UI and error reporting
• Limited reflection API
• Alternatives
• FxCop
• PostSharp Architecture Framework
Architecture Unit Tests
@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
@gfraiteur
All design patterns are found in books
MYTH 5
@gfraiteur
spinning into
Emergent Design
@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
@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
@gfraiteur
Iterative Design
• Unit Tests• Load Tests• Integration
Tests
• Project• Team• Design
• Abstract• Design• Refactor• Implement
• Time• User Stories
Planning Development
TestingReview
@gfraiteur
Agile & Continuous Improvement
Design
RefactorImplement new stuff
Learn
KnowledgePatternsLanguage
Requirements
Code
@gfraiteur
We’re a part of a whole
Software EngineeringDiscipline
Company / Personal
Know-How
Project
EngineeringDiscipline
@gfraiteurHolašovice, Czech Republic
Patterns emerge naturally from iterative design processes.
TRUTH 5
@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))]
@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
@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
@gfraiteur
ReaderWriterSynchronized with PostSharp
LAB 4
@gfraiteur
Q&AGael [email protected]@gfraiteur
@gfraiteurCopyright © by Arild Vågen
Summary• Compilers must do more to raise abstraction
level
• Patterns stem from continuous improvement
BETTER SOFTWARE THROUGH SIMPLER CODE