KEMBAR78
Evolution of Patterns | PPTX
The Evolution of Patterns
    Influences and Examples of Emerging Patterns

                                        Chris Eargle
Chris Eargle
Telerik Developer Evangelist

INETA Director

Microsoft MVP – Visual C#

MCPD, MCTS, etc

kodefuguru.com

kodefuguru@live.com
What is a Pattern?
      An element of reusable software

      Names, motivates, and explains a general design that
      addresses recurring design problems

      Describes the problem, the solution, when to apply the
      solution, and its consequences




3/17/2012                                          3
Implementation Levels
Invisible   Part of the language

Informal    Referred to by name, but must be re-
            implemented for each use

Formal      Implement pattern itself within the
            language
Influences
Language Features

Innovations in Object-Oriented Design

Other Programming Paradigms
Language
Design patterns may be a sign of missing language
features
“16 of 23 patterns have qualitatively
 simpler implementation in Lisp or
Dylan than in C++ for at least some
       uses of each pattern.”
              Peter Norvig
Language Features
Keywords are added to languages

Typically streamlines a process

May be tied to other paradigms
Innovations
Object-Oriented Programming is mature

Sometimes an idea takes hold…
           challenges OO Principles…
                 and takes the community by storm.
Fluent Interfaces
Utilizes method chaining

Defined through the return value of a called method

Self referential - the new context is equivalent to the
last context
Fluent Interfaces
English-readable, left-to-right code

May violate command-query separation with stateful
objects

Terminated through the return of a void context
Paradigms
There is a huge ecosystem of programming languages

Most languages fall into categories other than OO

These slowly make their way into modern OO
languages
Generic
Algorithms use types to be specified later

Reduces duplication

Pioneered by ADA in 1983

May be harder to understand
Terminology
Generics
     Ada, Eiffel, Java, C#, F#, and Visual Basic .NET

Parametric Polymorphism
     ML, Scala and Haskell

Templates
     C++
Functional
Treats computation as the evaluation of functions

Avoids state and mutable data

Roots in lambda calculus
Terminology
first class functions
        functions can be passed as arguments

higher-order functions
      take one or more functions as input
      or outputs a function

combinators
     takes one or more functions as input
     and outputs a function
“a combinator is a function which
 builds program fragments from
      program fragments…”

            John Hughes
Terminology
pure functions
      no side effects, which means the function called
      with the same arguments returns the same result

anonymous functions
     a function defined, and possibly called, without
     being bound to an identifier.

continuation-passing style
      when a function is complete, the computation is
      passed to a continuation function
Singleton
Singleton Pattern
      Restricts class instantiation to one instance

      Useful when only one instance is needed in system

      Some consider this an anti-pattern

      Not the same as a static class




3/17/2012                                             20
Singleton Diagram

        Singleton
                      Retrieves a single,
      <<singleton>>   maintained instance
                      of class.
    - Singleton()
    S+ Instance
Influence on Language
A few modern languages directly implement this
construct
Scala Example

// Singleton uses the object keyword instead of class
object Utilities {
  val test = "Test App"

    def loadImages() = {
      // ...
    }
    def createManager():EntityManager = {
      // ...
    }
}
Lazy Initialization
Lazy Initialization
Delays creation of an object

Use when eager initialization is resource intensive

Often used with a factory method (more later)
Iterator
Iterator Pattern
iterator    an object that provides a standard way to
            examine all element of any collection.

Has a uniform interface for traversing many data
structure without exposing their implementations.

Supports concurrent iteration and element removal.

No need to know the internal structure of collection.
Iterator Diagram
    Aggregate         Client         Iterator
   <<interface>>                  <<interface>>

+ CreateIterator()             + First()
                               + Next()
                               + IsDone()
                               + CurrentItem



  ConcreteAggregate             ConcreteIterator


+ CreateIterator()
Command
Command Pattern
used to represent and encapsulate all the information
needed to call a method at a later time
Terminology
client
         instantiates the command object and provides
         the information required to call the method at a
         later time.

invoker
      decides when the method should be called

receiver
      instance of the class that contains the method's
      code
Command Diagram
            Invoker          Command
                            <<interface>>

                        + Execute()


 Invoker




            Receiver   ConcreteCommand

   + Action()          + Execute()
Fluent Command
      The Execute method return the object

      Enables quick access to return properties



            command.Execute();               var results = command.Execute()
            var results = command.Results;                        .Results;




3/17/2012                                                             33
Delegate Command
Execute is accepted as a function
Factory
Factory Method
Method used to create an object

Used when it is more complex to simply instantiate

Don’t confuse this with abstract factory, a generalized
pattern for building a series of factories

Useful in C# to take advantage of generic inference
Function Factory
Combinators correspond to factory methods for
functions
Factory Function
Utilizes a function to create an object
Strategy
Strategy Pattern
Defines family of algorithms

Encapsulates algorithms to make them
interchangeable
Strategy Diagram
                          Containment          Strategy
    Context                                  <<interface>>
ContextInterface()            Use          AlgorithmInterface()




                          <<implements>>                      <<implements>>


                     ConcreteStrategyA                      ConcreteStrategyB
                     <<implementation>>                     <<implementation>>

                      AlgorithmInterface()                    AlgorithmInterface()
Function Strategy
      When a strategy is required, allow owner to pass in a
      function

      Prevents class explosion

      Enables flexibility




3/17/2012                                           42
Function Strategy

                      Containment     Strategy
     Context                        <<delegate>>
 ContextInterface()      Use
Template Method
Template Pattern
Defines the program skeleton of an algorithm

Encapsulate what changes and make it abstract

Subclasses implement those steps in the algorithm
Template Diagram
         AbstractClass
     A+ PrimitiveOperation1()
     A+ PrimitiveOperation2 ()
     + TemplateMethod()




         ConcreteClass

      + PrimitiveOperation1()
      + PrimitiveOperation2()
Function Template
      Encapsulate what changes into functions

      Allow owner to specify those pieces of the algorithm

      Prevents class explosion




3/17/2012                                          47
References
Online references and source code can be found at
bitly.com/wHIBI0
References
Patterns of Enterprise          Functional thinking: Functional
Application Architecture        design patterns, Part 1
               -Martin Fowler                  -Neal Ford

Revenge of the Nerds            Generalising Monads to Arrows
              -Paul Graham                    -John Hughes

Object World, May 5 1996        Design Patterns: Elements of
               -Peter Norvig    Reusable Object-Oriented
                                Software
                                              -GoF

Evolution of Patterns

  • 1.
    The Evolution ofPatterns Influences and Examples of Emerging Patterns Chris Eargle
  • 2.
    Chris Eargle Telerik DeveloperEvangelist INETA Director Microsoft MVP – Visual C# MCPD, MCTS, etc kodefuguru.com kodefuguru@live.com
  • 3.
    What is aPattern? An element of reusable software Names, motivates, and explains a general design that addresses recurring design problems Describes the problem, the solution, when to apply the solution, and its consequences 3/17/2012 3
  • 4.
    Implementation Levels Invisible Part of the language Informal Referred to by name, but must be re- implemented for each use Formal Implement pattern itself within the language
  • 5.
    Influences Language Features Innovations inObject-Oriented Design Other Programming Paradigms
  • 6.
    Language Design patterns maybe a sign of missing language features
  • 7.
    “16 of 23patterns have qualitatively simpler implementation in Lisp or Dylan than in C++ for at least some uses of each pattern.” Peter Norvig
  • 8.
    Language Features Keywords areadded to languages Typically streamlines a process May be tied to other paradigms
  • 9.
    Innovations Object-Oriented Programming ismature Sometimes an idea takes hold… challenges OO Principles… and takes the community by storm.
  • 10.
    Fluent Interfaces Utilizes methodchaining Defined through the return value of a called method Self referential - the new context is equivalent to the last context
  • 11.
    Fluent Interfaces English-readable, left-to-rightcode May violate command-query separation with stateful objects Terminated through the return of a void context
  • 12.
    Paradigms There is ahuge ecosystem of programming languages Most languages fall into categories other than OO These slowly make their way into modern OO languages
  • 13.
    Generic Algorithms use typesto be specified later Reduces duplication Pioneered by ADA in 1983 May be harder to understand
  • 14.
    Terminology Generics Ada, Eiffel, Java, C#, F#, and Visual Basic .NET Parametric Polymorphism ML, Scala and Haskell Templates C++
  • 15.
    Functional Treats computation asthe evaluation of functions Avoids state and mutable data Roots in lambda calculus
  • 16.
    Terminology first class functions functions can be passed as arguments higher-order functions take one or more functions as input or outputs a function combinators takes one or more functions as input and outputs a function
  • 17.
    “a combinator isa function which builds program fragments from program fragments…” John Hughes
  • 18.
    Terminology pure functions no side effects, which means the function called with the same arguments returns the same result anonymous functions a function defined, and possibly called, without being bound to an identifier. continuation-passing style when a function is complete, the computation is passed to a continuation function
  • 19.
  • 20.
    Singleton Pattern Restricts class instantiation to one instance Useful when only one instance is needed in system Some consider this an anti-pattern Not the same as a static class 3/17/2012 20
  • 21.
    Singleton Diagram Singleton Retrieves a single, <<singleton>> maintained instance of class. - Singleton() S+ Instance
  • 22.
    Influence on Language Afew modern languages directly implement this construct
  • 23.
    Scala Example // Singletonuses the object keyword instead of class object Utilities { val test = "Test App" def loadImages() = { // ... } def createManager():EntityManager = { // ... } }
  • 24.
  • 25.
    Lazy Initialization Delays creationof an object Use when eager initialization is resource intensive Often used with a factory method (more later)
  • 26.
  • 27.
    Iterator Pattern iterator an object that provides a standard way to examine all element of any collection. Has a uniform interface for traversing many data structure without exposing their implementations. Supports concurrent iteration and element removal. No need to know the internal structure of collection.
  • 28.
    Iterator Diagram Aggregate Client Iterator <<interface>> <<interface>> + CreateIterator() + First() + Next() + IsDone() + CurrentItem ConcreteAggregate ConcreteIterator + CreateIterator()
  • 29.
  • 30.
    Command Pattern used torepresent and encapsulate all the information needed to call a method at a later time
  • 31.
    Terminology client instantiates the command object and provides the information required to call the method at a later time. invoker decides when the method should be called receiver instance of the class that contains the method's code
  • 32.
    Command Diagram Invoker Command <<interface>> + Execute() Invoker Receiver ConcreteCommand + Action() + Execute()
  • 33.
    Fluent Command The Execute method return the object Enables quick access to return properties command.Execute(); var results = command.Execute() var results = command.Results; .Results; 3/17/2012 33
  • 34.
    Delegate Command Execute isaccepted as a function
  • 35.
  • 36.
    Factory Method Method usedto create an object Used when it is more complex to simply instantiate Don’t confuse this with abstract factory, a generalized pattern for building a series of factories Useful in C# to take advantage of generic inference
  • 37.
    Function Factory Combinators correspondto factory methods for functions
  • 38.
    Factory Function Utilizes afunction to create an object
  • 39.
  • 40.
    Strategy Pattern Defines familyof algorithms Encapsulates algorithms to make them interchangeable
  • 41.
    Strategy Diagram Containment Strategy Context <<interface>> ContextInterface() Use AlgorithmInterface() <<implements>> <<implements>> ConcreteStrategyA ConcreteStrategyB <<implementation>> <<implementation>> AlgorithmInterface() AlgorithmInterface()
  • 42.
    Function Strategy When a strategy is required, allow owner to pass in a function Prevents class explosion Enables flexibility 3/17/2012 42
  • 43.
    Function Strategy Containment Strategy Context <<delegate>> ContextInterface() Use
  • 44.
  • 45.
    Template Pattern Defines theprogram skeleton of an algorithm Encapsulate what changes and make it abstract Subclasses implement those steps in the algorithm
  • 46.
    Template Diagram AbstractClass A+ PrimitiveOperation1() A+ PrimitiveOperation2 () + TemplateMethod() ConcreteClass + PrimitiveOperation1() + PrimitiveOperation2()
  • 47.
    Function Template Encapsulate what changes into functions Allow owner to specify those pieces of the algorithm Prevents class explosion 3/17/2012 47
  • 48.
    References Online references andsource code can be found at bitly.com/wHIBI0
  • 49.
    References Patterns of Enterprise Functional thinking: Functional Application Architecture design patterns, Part 1 -Martin Fowler -Neal Ford Revenge of the Nerds Generalising Monads to Arrows -Paul Graham -John Hughes Object World, May 5 1996 Design Patterns: Elements of -Peter Norvig Reusable Object-Oriented Software -GoF

Editor's Notes

  • #5 These three levels are from Peter Norvig’s articles in Object World.
  • #14 generic programming is a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters. This approach, pioneered by Ada in 1983, permits writing common functions or types that differ only in the set of types on which they operate when used, thus reducing duplication. The Gang of Four noted that this technique, especially when combined with delegation, is very powerful but that &quot;[dynamic], highly parameterized software is harder to understand than more static software.&quot;[1]