Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Design Pattern Automation

2 258 vues

Publié le

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.

Publié dans : Technologie
  • Identifiez-vous pour voir les commentaires

Design Pattern Automation

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

×