Download - Evolution of Patterns

Transcript
Page 1: Evolution of Patterns

The Evolution of PatternsInfluences and Examples of Emerging Patterns

Chris Eargle

Page 2: Evolution of Patterns

Chris EargleTelerik Developer Evangelist

INETA Director

Microsoft MVP – Visual C#

MCPD, MCTS, etc

kodefuguru.com

[email protected]

Page 3: Evolution of Patterns

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

304/11/2023

Page 4: Evolution of Patterns

Implementation LevelsInvisible Part of the language

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

Formal Implement pattern itself within the language

Page 5: Evolution of Patterns

InfluencesLanguage Features

Innovations in Object-Oriented Design

Other Programming Paradigms

Page 6: Evolution of Patterns

LanguageDesign patterns may be a sign of missing language features

Page 7: Evolution of Patterns

“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

Page 8: Evolution of Patterns

Language FeaturesKeywords are added to languages

Typically streamlines a process

May be tied to other paradigms

Page 9: Evolution of Patterns

InnovationsObject-Oriented Programming is mature

Sometimes an idea takes hold…

challenges OO Principles…

and takes the community by storm.

Page 10: Evolution of Patterns

Fluent InterfacesUtilizes method chaining

Defined through the return value of a called method

Self referential - the new context is equivalent to the last context

Page 11: Evolution of Patterns

Fluent InterfacesEnglish-readable, left-to-right code

May violate command-query separation with stateful objects

Terminated through the return of a void context

Page 12: Evolution of Patterns

ParadigmsThere is a huge ecosystem of programming languages

Most languages fall into categories other than OO

These slowly make their way into modern OO languages

Page 13: Evolution of Patterns

GenericAlgorithms use types to be specified later

Reduces duplication

Pioneered by ADA in 1983

May be harder to understand

Page 14: Evolution of Patterns

TerminologyGenerics

Ada, Eiffel, Java, C#, F#, and Visual Basic .NET

Parametric Polymorphism

ML, Scala and Haskell

Templates

C++

Page 15: Evolution of Patterns

FunctionalTreats computation as the evaluation of functions

Avoids state and mutable data

Roots in lambda calculus

Page 16: Evolution of Patterns

Terminologyfirst 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

Page 17: Evolution of Patterns

“a combinator is a function which builds program fragments from

program fragments…”

John Hughes

Page 18: Evolution of Patterns

Terminologypure 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

Page 19: Evolution of Patterns

Singleton

kodefuguru
Photo credits: http://www.flickr.com/photos/greyworld/6213475995
Chris Eargle
http://www.flickr.com/photos/22179048@N05/5144031942/
Page 20: Evolution of Patterns

Singleton PatternRestricts 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

2004/11/2023

Page 21: Evolution of Patterns

Singleton Diagram

- Singleton()S+ Instance

Singleton<<singleton>>

Retrieves a single, maintained instance of class.

Page 22: Evolution of Patterns

Influence on LanguageA few modern languages directly implement this construct

Page 23: Evolution of Patterns

Scala Example

// Singleton uses the object keyword instead of classobject Utilities { val test = "Test App" def loadImages() = { // ... } def createManager():EntityManager = { // ... }}

Page 24: Evolution of Patterns

Lazy Initialization

kodefuguru
Photo credits: http://www.flickr.com/photos/greyworld/6213475995
Chris Eargle
http://www.flickr.com/photos/brandonthemandon/2851417514/
Page 25: Evolution of Patterns

Lazy InitializationDelays creation of an object

Use when eager initialization is resource intensive

Often used with a factory method (more later)

Page 26: Evolution of Patterns

Iterator

kodefuguru
Photo credits: http://www.flickr.com/photos/greyworld/6213475995
Page 27: Evolution of Patterns

Iterator Patterniterator 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.

Page 28: Evolution of Patterns

Iterator Diagram

+ CreateIterator()

Aggregate<<interface>>

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

Iterator<<interface>>

+ CreateIterator()

ConcreteAggregate ConcreteIterator

Client

Page 29: Evolution of Patterns

Command

Chris Eargle
http://www.flickr.com/photos/mwichary/3300657149/
Page 30: Evolution of Patterns

Command Patternused to represent and encapsulate all the information needed to call a method at a later time

Page 31: Evolution of Patterns

Terminologyclient

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

Page 32: Evolution of Patterns

Command DiagramInvoker

+ Execute()

Command<<interface>>

+ Action()

Receiver

+ Execute()

ConcreteCommand

Invoker

Page 33: Evolution of Patterns

Fluent CommandThe Execute method return the object

Enables quick access to return properties

3304/11/2023

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

var results = command.Execute()

.Results;

Page 34: Evolution of Patterns

Delegate CommandExecute is accepted as a function

Page 35: Evolution of Patterns

Factory

kodefuguru
Attrib: http://www.flickr.com/photos/marcwathieu/5184546266/
Page 36: Evolution of Patterns

Factory MethodMethod 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

Page 37: Evolution of Patterns

Function FactoryCombinators correspond to factory methods for functions

Page 38: Evolution of Patterns

Factory FunctionUtilizes a function to create an object

Page 39: Evolution of Patterns

Strategy

kodefuguru
Photo credits: http://www.flickr.com/photos/calliope/5347237755
Page 40: Evolution of Patterns

Strategy PatternDefines family of algorithms

Encapsulates algorithms to make them interchangeable

Page 41: Evolution of Patterns

Strategy Diagram

ContextInterface()

Context

AlgorithmInterface()

Strategy<<interface>>

AlgorithmInterface()

ConcreteStrategyB<<implementation>>

AlgorithmInterface()

ConcreteStrategyA<<implementation>>

<<implements>> <<implements>>

Containment

Use

Page 42: Evolution of Patterns

Function StrategyWhen a strategy is required, allow owner to pass in a function

Prevents class explosion

Enables flexibility

4204/11/2023

Page 43: Evolution of Patterns

Function Strategy

ContextInterface()

ContextStrategy

<<delegate>>Containment

Use

Page 44: Evolution of Patterns

Template Method

kodefuguru
Photo credits: http://www.flickr.com/photos/mpastwa/2110418928/
Page 45: Evolution of Patterns

Template PatternDefines the program skeleton of an algorithm

Encapsulate what changes and make it abstract

Subclasses implement those steps in the algorithm

Page 46: Evolution of Patterns

Template Diagram

A+ PrimitiveOperation1()A+ PrimitiveOperation2 ()+ TemplateMethod()

AbstractClass

+ PrimitiveOperation1()+ PrimitiveOperation2()

ConcreteClass

Page 47: Evolution of Patterns

Function TemplateEncapsulate what changes into functions

Allow owner to specify those pieces of the algorithm

Prevents class explosion

4704/11/2023

Page 48: Evolution of Patterns

ReferencesOnline references and source code can be found at bitly.com/wHIBI0

Page 49: Evolution of Patterns

ReferencesPatterns of Enterprise Application Architecture

-Martin Fowler

Revenge of the Nerds

-Paul Graham

Object World, May 5 1996

-Peter Norvig

Functional thinking: Functional design patterns, Part 1

-Neal Ford

Generalising Monads to Arrows

-John Hughes

Design Patterns: Elements of Reusable Object-Oriented Software

-GoF