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
![Page 1: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/1.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/2.jpg)
@gfraiteur
Hello!
My name is GAEL.
No, I don’t thinkmy accent is funny.
my twitter
![Page 3: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/3.jpg)
@gfraiteur
My commitment to you:to seed a vision, not to sell a tool.
![Page 4: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/4.jpg)
@gfraiteur
The vision:Code at the right level of abstraction,with compiler-supported design patterns
![Page 5: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/5.jpg)
@gfraiteur
NotifyPropertyChanged,the wrong way
LAB 1
![Page 6: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/6.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/7.jpg)
@gfraiteur
Univac Assembly Language
![Page 8: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/8.jpg)
@gfraiteur
FORTRAN (1955)• 1955 - FORTRAN I
• Global Variables
• Arrays
• 1958 - FORTRAN II
• Procedural Programming(no recursion)
• Modules
![Page 9: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/9.jpg)
@gfraiteur
COBOL (1959)• Data Structures
• Natural Language
![Page 10: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/10.jpg)
@gfraiteur
LISP (1959)• Variable Scopes (Stack)
• Garbage Collection
![Page 11: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/11.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/12.jpg)
@gfraiteurCopyright © by Arild Vågen
Human Language
Cognition
Communication
Social Organization
Machine Language
Execution
Programming Languages
![Page 13: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/13.jpg)
@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)
![Page 14: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/14.jpg)
@gfraiteur
A world of patterns
LANGUAGE
REGULARITYRESOLUTION
OF FORCESSELF-
SUSTAINABILITY
VARIETY UBIQUITY
![Page 15: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/15.jpg)
@gfraiteurLanguage Enables Cognition
![Page 16: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/16.jpg)
@gfraiteurCopyright © Stijn NieuwendijkLanguage Enables Communication
![Page 17: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/17.jpg)
@gfraiteur
Language for the Trade
![Page 18: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/18.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/19.jpg)
@gfraiteur
Software Patterns (1995)• Apply to OO design
• Canonic pattern description
• First catalogue of 23 patterns
![Page 20: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/20.jpg)
@gfraiteur
In 2013, eighteen years later, design patterns still live in their parent’s house – books.
![Page 21: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/21.jpg)
@gfraiteur
Latent Redundancy
Bad OO code Good OO code Ideal code
Intrinsic variability Latent redundancy Bad redundancy
![Page 22: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/22.jpg)
@gfraiteur
Cost of low abstraction• Boilerplate code
• More bugs
• Code review
• Changing the pattern implementation
![Page 23: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/23.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/24.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/25.jpg)
@gfraiteur
Pattern-Aware Compilers allow for coding at the level of abstraction of design patterns.
![Page 26: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/26.jpg)
@gfraiteur
Design Pattern Automation is new
© hellraiser
MYTH 1
![Page 27: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/27.jpg)
@gfraiteur
Pattern C# Keyword
Dispose using
Lock lock
Publish-Subscribe event
C# implements several patternsTRUTH 1
![Page 28: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/28.jpg)
@gfraiteur
But C# does not let you implement your own patterns.
![Page 29: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/29.jpg)
@gfraiteur
code generators were proved wrong
You can’t automate patterns because…
MYTH 2
![Page 30: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/30.jpg)
@gfraiteur
pattern-aware programming languages and compilers
we don’t need code generators but
TRUTH 2
![Page 31: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/31.jpg)
@gfraiteur
there are too many skills involved
You can’t automate patterns because…
MYTH 3
![Page 32: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/32.jpg)
@gfraiteur
industrial revolutionSkill scarcity motivated the
TRUTH 3
![Page 33: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/33.jpg)
@gfraiteur
Our own industrial revolution:
Making it easier to build good software
![Page 34: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/34.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/35.jpg)
@gfraiteur
they are way too complex general abstract
You can’t automate patterns because…
MYTH 4
![Page 36: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/36.jpg)
@gfraiteur
a normal part of any engineering process
Making abstract things concrete is
TRUTH 4
ConcreteAbstract
Automatable
Non-Automatable
![Page 37: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/37.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/38.jpg)
@gfraiteur
Example: Observer Pattern
1. Vanilla Design Pattern (GoF)
![Page 39: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/39.jpg)
@gfraiteur
Example: Observer Pattern
1
2
3Call OnPropertyChanged whenever a property has changed.
2. Language-Specific Pattern (C#)
![Page 40: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/40.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/41.jpg)
@gfraiteur
Example: Observer Pattern4. Source code
![Page 42: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/42.jpg)
@gfraiteur
NotifyPropertyChangedwith PostSharp
LAB 2
![Page 43: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/43.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/44.jpg)
@gfraiteur
Transformation
Verification
![Page 45: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/45.jpg)
@gfraiteur
Pattern Automation Technologies
Pattern Automati
on
![Page 46: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/46.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/47.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/48.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/49.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/50.jpg)
@gfraiteur
Macro-enabled .NET languages• Nemerle
• Scala
• Boo
![Page 51: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/51.jpg)
@gfraiteur
Poorman’s Pattern
Automation: Static Analysis
Pattern Automati
on
![Page 52: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/52.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/53.jpg)
@gfraiteur
Singleton with Architecture Unit Tests
LAB 3
![Page 54: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/54.jpg)
@gfraiteur
• Limitations
• Inadequate UI and error reporting
• Limited reflection API
• Alternatives
• FxCop
• PostSharp Architecture Framework
Architecture Unit Tests
![Page 55: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/55.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/56.jpg)
@gfraiteur
All design patterns are found in books
MYTH 5
![Page 57: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/57.jpg)
@gfraiteur
spinning into
Emergent Design
![Page 58: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/58.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/59.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/60.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/61.jpg)
@gfraiteur
Agile & Continuous Improvement
Design
RefactorImplement new stuff
Learn
KnowledgePatternsLanguage
Requirements
Code
![Page 62: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/62.jpg)
@gfraiteur
We’re a part of a whole
Software EngineeringDiscipline
Company / Personal
Know-How
Project
EngineeringDiscipline
![Page 63: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/63.jpg)
@gfraiteurHolašovice, Czech Republic
Patterns emerge naturally from iterative design processes.
TRUTH 5
![Page 64: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/64.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/65.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/66.jpg)
@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](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/67.jpg)
@gfraiteur
ReaderWriterSynchronized with PostSharp
LAB 4
![Page 69: Design Pattern Automation](https://reader035.vdocument.in/reader035/viewer/2022062618/54d7e6094a795989138b4583/html5/thumbnails/69.jpg)
@gfraiteurCopyright © by Arild Vågen
Summary• Compilers must do more to raise abstraction
level
• Patterns stem from continuous improvement
BETTER SOFTWARE THROUGH SIMPLER CODE