139
Elemental Design Patterns Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Embed Size (px)

Citation preview

Page 1: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Elemental Design Patterns

Jason SmithThe Software Revolution, Inc.

based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Page 2: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Decorator (GOF)

Page 3: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Used to extend behavior dynamically, at run time

Like an internal plug-in system as found in a web browser

Alternative to using inheritance to provide all possible combinations of behaviors

Decorator informally

Page 4: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Decompose DecoratorObjectifier

Zimmer, 1995

Page 5: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Allows a single object interface to represent multiple concrete implementations

Client requests a method to be invoked via the objectifier interface

Client cannot know which of the concrete method bodies will execute and provide the service

Objectifier

Page 6: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Decompose DecoratorObject Recursion

Woolf, 1998

Page 7: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Chains two objects with related types

Object recursion uses Objectifier as the backbone of its form…

Adds a link between one of the concrete implementations and the interface

Uses the link to invoke the same method --to “recurse” in a sense, but on a different (related) object

Object Recursion

Page 8: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

So Decorator can be described in

terms of composing smaller pieces

Are these pieces as small as they can

be?

Perhaps there is more “downward” to

go

Where is the bottom of this abstraction

pile?

Turtles all the way down

Page 9: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Turtles all the way down

So Decorator can be described in

terms of composing smaller pieces

Are these pieces as small as they can

be?

Perhaps there is more “downward” to

go

Where is the bottom of this abstraction

pile?

Page 10: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Turtles all the way down

So Decorator can be described in

terms of composing smaller pieces

Are these pieces as small as they can

be?

Perhaps there is more “downward” to

go

Where is the bottom of this abstraction

pile?

EDP

Page 11: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Turtles all the way down

So Decorator can be described in

terms of composing smaller pieces

Are these pieces as small as they can

be?

Perhaps there is more “downward” to

go

Where is the bottom of this abstraction

pile?

EDP

Elemental design patterns

decorator

object recursio

n

objectifier

Page 12: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Pattern Descriptions

Solution

• structure• implementation• sample code

Problem

• intent• motivation• known uses

Context

• applicability• consequences• related patterns

Hard to fit: participants, collaborations

Design Pattern A common solution to a common problem within a particular context

Page 13: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Participants, Collaborations are parts and relationships

Relationships form the core of design

Design of a car is more than a piles of pieces… engine, tires, transmission, seats

Design shows how the parts relate… connect, interact, work of one affects function of another

Parts list gives components

Relationships tell how parts function in concert to win over the entropy of the pile

Relationships

Page 14: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

What is smallest relationship we can define?

A single concept connection between two things

Look for such among the entities in OO programming

Means we have to decide…

what are the conceptual entities in OO programs?

Relationships

Page 15: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Goal: detect elemental relationships automatically, if we can

Goal: compose elemental patterns to get higher patterns (automatically, if we can)

Higher pattern means more complex, harder to find patterns

Pause… for directions

Page 16: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Different relationships, different purposes

Scoping relationships give context

Scope is how an element is made unique among all other elements in the system

class Menu in package GUI_Elements is not the same thing as

class Menu in package Restaurant_Items

Scope: an enclosing something that has a name, in which you define something new

Contextual Relationships

Page 17: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Class: scope for methods and fields it defines

Package/namespace: scope for all in it

Method/function: scope for local variables

Access an element: specify the scopes from outer level in

◦ Implicit notation: No scope for locals in a method, or another method in same class

◦ Differing notation:

GUI_Element::Menu vs. aMenu.anItem

Context: Scope

Page 18: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

We now have scope relationships… what else can we form relationships between?

Classes, their fields and methods…. not much else

What about objects? Different from classes?

Classes do two things

◦ Type information… member methods and fields that will exists separately in each object created

◦ Global shared fields… “static” class methods and fields

Class is really a type with an object (for global)

Relationships… what else?

Page 19: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Fields, Methods, Objects, Types OO entities we characterize relationships among

FMOT

Object Method Field Type

Object Defines Defines Defines Defines or Is of type

Method N/A Defines orMethod call

Defines orField use

Defines orReturns of type

Field N/A State change

Cohesion Is of type

Type Defines Defines Defines Defines orsubtyping

Page 20: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Fields, Methods, Objects, Types Defines is a scope relationship

FMOT

Object Method Field Type

Object Defines Defines Defines Defines or Is of type

Method N/A Defines orMethod call

Defines orField use

Defines orReturns of type

Field N/A State change

Cohesion Is of type

Type Defines Defines Defines Defines orsubtyping

Page 21: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

OO Entity InteractionsFMOT Non-scoping

Object Method Field Type

ObjectIs of type

MethodMethod call Field use Returns of

type

Field State change

Cohesion Is of type

Typesubtyping

Page 22: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Terminologymethod call

o . f( ) calls p . g( )enclosingobject

enclosingobject

Callingmethod

Calledmethod

Page 23: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

EDP Basicsin UML

A B

b: B

f()

g()h()

b.g()

Page 24: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

class A { B b; f ( ) { b.g(); }}class B { g ( ) { } h ( ) { }}main ( ) { A a; a.f();}

EDP Basicsin Code

Page 25: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

More EDPsDeputized Redirection

Page 26: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

More EDPsRecursion

Page 27: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

First Four EDPs

dissimilar

Conglomeration

Recursion

Delegation

Redirection

similar

dissimilar

similar

methodsimilarity

object similarit

y

Page 28: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

s-calculus ◦ Abadi and Cardelli, “A Theory of Objects”, 1998◦ Computation model for OO programs◦ Object form of l-calculus

r-calculus◦ Modification and extension for patterns◦ Operators for reliances◦ J. Smith, 2004

Theoretical Foundations

Page 29: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Another Example

Page 30: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Focus on OO programming concepts, not OO language constructs

a.f() calls b.g(), then b.g() calls c.h() We can see that a.f() does not call c.h() (a

structural relationship)

However a.f() relies on c.h() to execute correctly in order for f to complete its work

So there is a reliance between a.f() and c.h() (conceptual, not structural)

Reliances

Page 31: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Zimmer, W. Relationships between design patterns. In J.O. Coplien and D.C. Schmidt, eds., Pattern Languages of Program Design. Addison-Wesley, Voston, 1995, pp. 345-364.

Woolf, B. The object recursion pattern. In N. Harrison, B. Foote, and H. Rohnert, eds., Pattern Languages of Program Design 4. Addison-Wesley, Boston, 1998, pp. 41-52.

References

Page 32: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Decorator (GOF)

Page 33: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Decorator in PIN

Page 34: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Abstract Factory

Page 35: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Factory Method

Page 36: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Decorator

Page 37: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Proxy

Page 38: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Chain of Responsibility

Page 39: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Template Method

Page 40: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012
Page 41: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Create Object

Page 42: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Retrieve

Page 43: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Inheritance

Page 44: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Abstract Interface

Page 45: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Delegation

Page 46: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Redirection

Page 47: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Conglomeration

Page 48: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Recursion

Page 49: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Revert Method

Page 50: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Extend Method

Page 51: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Delegated Conglomeration

Page 52: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Redirected Recursion

Page 53: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Trusted Delegation

Page 54: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Trusted Redirection

Page 55: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Deputized Delegation

Page 56: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Deputized Redirection

Page 57: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012
Page 58: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Fulfill Method

Page 59: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Retrieve New

Page 60: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Retrieve Shared

Page 61: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Objectifier

Page 62: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Object Recursion

Page 63: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012
Page 64: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Similarity Space A1

Page 65: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Similarity Space A2

Page 66: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Standard PINbox

Page 67: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Expanded PIN Instance

Page 68: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012
Page 69: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

SPQR Flow

Page 70: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012
Page 71: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Analysis of Design Pattern Structure in OO Source

Code

Jason SmithIBM Watson Research

Page 72: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Our Goals

• Provide tools to software engineers to aid in the efficient comprehension of existing code bases (maintenance, new design with re-use)

• Provide support for development of new code that adheres to best-practices architecture

• Create metrics to compare relative comprehensibility and relative quality of system designs for analysis prior to implementation

• Why find patterns?

Page 73: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Design Patterns

• Abstractions of lessons learned

• GoF 1995

• Culling code down to the language-independent ideas, not implementation specifics …

• Community agreement on what gains “pattern” status

• Common vocabulary for software engineers to discuss and compare design issues, best practices, architecture and organization

“Common (agreed good) solutions to common problems”

Page 74: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Atop the Abstraction Stack

• Assembly mnemonics

• Procedural programming -- locality of code

• Object-oriented programming -- encapsulation of data with code

• Idioms

best language practices

• Design patterns

best design practices, language independent

Page 75: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Example Pattern“Composite”

Page 76: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Example Pattern“Composite”

Page 77: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

In reality, they look like this …

// Composite pattern -- Structural example using System;using System.Text;using System.Collections;

// "Component"abstract class Component{  // Fields  protected string name; // Constructors  public Component( string name )  { this.name = name; } // Methods  abstract public void Add(Component c);  abstract public void Remove( Component c );  abstract public void Display( int depth );}

// "Composite"Class Composite : Component{  // Fields  private ArrayList children = new ArrayList();  // Constructors  public Composite( string name ) : base( name ) {}  // Methods  public override void Add( Component component )  { children.Add( component ); }  public override void Remove( Component component )  { children.Remove( component );}  public override void Display( int depth )  { Console.WriteLine( new String( '-', depth ) + name );    // Display each of the node's children    foreach( Component component in children )      component.Display( depth + 2 );  }}

// "Leaf"class Leaf : Component{ // Constructors  public Leaf( string name ) : base( name ) {}  // Methods  public override void Add( Component c )  { Console.WriteLine("Cannot add to a leaf"); }

  public override void Remove( Component c )  { Console.WriteLine("Cannot remove from a leaf"); }

  public override void Display( int depth )  { Console.WriteLine( new String( '-', depth ) + name ); }}

public class Client{  public static void Main( string[] args ) {    // Create a tree structure    Composite root = new Composite( "root" );    root.Add( new Leaf( "Leaf A" ));    root.Add( new Leaf( "Leaf B" ));    Composite comp = new Composite( "Composite X" );

    comp.Add( new Leaf( "Leaf XA" ) );    comp.Add( new Leaf( "Leaf XB" ) );    root.Add( comp );

    root.Add( new Leaf( "Leaf C" ));

    // Add and remove a leaf    Leaf l = new Leaf( "Leaf D" );    root.Add( l );    root.Remove( l );

    // Recursively display nodes    root.Display( 1 );  }}

Page 78: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

// Purpose. Composite // Strategy. Use recursive composition // to create a heterogeneous aggregate#include <string.h> // that can be treated homogeneously.enum NodeType { FileT, DirT }; //int g_indent = 0; // Benefit. No more type checking and // type casting (coupling between Dirclass File { // and File is gone, Dir is onlypublic: // coupled to abstract base class) File( char* n ) { type_ = FileT; strcpy( name_, n ); } class AbsFile { NodeType getType() { return type_; } public: void ls() { virtual void ls() = 0; for (int i=0; i < g_indent; i++) protected: cout << ' '; char name_[20]; cout << name_ << endl; } static int indent_;private: }; NodeType type_; int AbsFile::indent_ = 0; char name_[20];}; class File: public AbsFile { public:class Dir { File( char* n ) {public: strcpy( name_, n ); } Dir( char* n ) { type_ = DirT; void ls() { strcpy( name_, n ); total_ = 0; } for (int i=0; i < indent_; i++) NodeType getType() { return type_; } cout << ' '; void add( File* f ) { cout << name_ << endl; } files_[total_++] = f; }; } void ls() { class Dir : public AbsFile { for (int i=0; i < g_indent; i++) public: cout << ' '; Dir( char* n ) { cout << name_ << ":" << endl; strcpy( name_, n ); total_ = 0; } g_indent += 3; void add( AbsFile* f ) { for (int i=0; i < total_; i++) files_[total_++] = f; } if (files_[i]->getType() void ls() { == DirT) for (int i=0; i < indent_; i++) ((Dir*) files_[i])->ls(); cout << ' '; else cout << name_ << ":" << endl; files_[i]->ls(); indent_ += 3; g_indent -= 3; } for (int i=0; i < total_; i++)private: files_[i]->ls(); NodeType type_; indent_ -= 3; } char name_[20]; private: File* files_[10]; AbsFile* files_[10]; int total_; int total_;}; };

void main( void ) void main( void ){ { Dir one("1"), two("2"), thr("3"); Dir one("1"), two("2"), thr("3"); File a("a"), b("b"), c("c"), File a("a"), b("b"), c("c"), d("d"), e("e"); d("d"), e("e"); one.add( &a ); one.add( &a ); one.add( (File*) &two ); one.add( &two ); one.add( &b ); one.add( &b ); two.add( &c ); two.add( &c ); two.add( &d ); two.add( &d ); two.add( (File*) &thr ); two.add( &thr ); thr.add( &e ); thr.add( &e ); one.ls(); one.ls();} }

Or this…

Page 79: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Or this…

(defgeneric add-dependent (dm dependent &amp;optional recursivep) ;; see below for the optional args (:documentation &quot;Add DEPENDENT as a dependent of DM. Return DM&quot;))

(defgeneric delete-dependent (dm dependent &amp;optional recursivep) (:documentation &quot;Remove DEPENDENT from DM. Return DM&quot;))

;;; No DELETE-DEPENDENT-IF

(defgeneric map-dependents (f dm) (:documentation &quot;Map F over the dependents of DM. Return DM&quot;))

;;; No cursors.

(defgeneric make-collection-for-dependent-mixin (dm))

(defclass dependent-mixin () ;; something that has dependents. We expose the DEPENDENTS slot. ((dependents :reader dependents-of)))

(defmethod make-collection-for-dependent-mixin ((dm dependent-mixin)) (make-instance 'simple-childed-mixin))

(defmethod initialize-instance :after ((dm dependent-mixin) &amp;key) (setf (slot-value dm 'dependents)

(make-collection-for-dependent-mixin dm)))

(defmethod add-dependent ((dm dependent-mixin) dependee &amp;optional recursivep)

(declare (ignorable recursivep)) (add-child (dependents-of dm) dependee) dm)

(defmethod delete-dependent ((dm dependent-mixin) dependee &amp;optional recursivep)

(declare (ignorable recursivep)) (delete-child (dependents-of dm) dependee) dm)

(defmethod map-dependents (f (dm dependent-mixin)) (map-over f (dependents-of dm)) dm)

A solution must be language independent

Page 80: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Patterns “Intertwingled”

All non-trivial designs involve many cross-mixed patterns

Same class might be a component in 4 or 5 patterns

Page 81: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Prior Approaches

• GoF patterns are too large to formalize flexibly

• “Mini-patterns” have been tried but still at too large a graularity

• Cannot handle implementation variances, due to static definitions of patterns… no inference capabilities

Page 82: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Elemental Design Patterns

• GoF patterns are too large to formalize flexibly

• Divide and conquer… define “bricks” and “wall construction procedures”

• Call them Elemental Design Patterns ( EDP )

• Idea is for EDPs to be easy to find in code

• Use resolution theorem prover to do the “wall assembly”

• Add to the assembly methods rules that allow flexible variations on basic definitions

Page 83: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Mini-Pattern: Objectifier

Page 84: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Mini-Pattern: Object Recursion

Page 85: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Relationships

Relationships are the key

Method calls: relationships between functions

Field access: relationships between objects

Inheritance: relationships between types

Objects, Types, Methods, Fields…

What else is there in OO? That’s it…

Page 86: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

SPQR System

Source code

gcc parse treegcc

object XML

gcc2poml

poml2otter

Source-code-specific otter clauses

Rho calculus compos rules EDP catalog

Otter theorem prover

Found patterns report

python

Otter proofs

Page 87: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

EDP Catalog

Object element EDPsCreateObj AbsInterface Retrieve

Type Relation EDPsInheritance

Method Invocation EDPsRecursion RedirectedRecursion Redirect RedirectInFamily RedirectInLimitedFamilyDelegate DelegateInFamily DelegateInLimitedFamilyConglomeration DelegatedConglomerationExtendMethod RevertMethod

Page 88: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Redirect In Family

Page 89: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Composing EDPs

• Successful in composing the EDPs to define 22 of the 23 GoF patterns

• Can produce many other described patterns as well from the literature

and mini’s… Objectifier, ObjRecursion

• 23rd GoF pattern is Iterator …Really a language construct, not a pattern

Is an operation for a data type

Page 90: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Formalization

• Begin with s-calculus ( OO analog to l-calculus )

• Add ability to encode relationships between constructs with reliance operators … gives r-calculus

Describe relationships between objects, methods, and fieldsTransitive

A<<B, B<<C : A<<C

• Define EDPs directly in r-calculus

• Express more complex patterns as compositions and assemblies of EDPs

• Encode it all as Otter clauses

Page 91: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

SPQR System

Source code

gcc parse treegcc

object XML

gcc2oml

oml2otter

Source-code-specific otter clauses

Rho calculus compos rules EDP catalog

Otter theorem prover

Found patterns report

python

Otter proofs

Page 92: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

“Isotopes”

• Flexibility requires capturing variations on a basic pattern

• Static definition/specification cannot capture the many different detailed forms code may take and still be judged a “pattern instance”

• Implementation may differ, clutter may be more or less, extra objects may be imposed, but

the base roles appear,

their relationships stay the same

Page 93: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Redirect In Family

Page 94: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Redirect In Family Isotope

Page 95: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Evaluation

• Code trials

• Problem from Evans and Sutherland

• C++ standard library analysis for EDPs

• Working code from previous projects (OvalTine)

Page 96: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

E&S Industrial Code

Page 97: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Rho Calculus Definitions

Page 98: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Found Patterns

Page 99: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Object Recursion - Annotated

Page 100: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Decorator Pattern

Page 101: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Decorator - Annotated

Page 102: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

SPQR Tool Sequence

Page 103: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Extension via Training

• Hundreds of patterns are named and in use

• How do we get these into the formal dictionary? Must a programmer be a r-calculus expert?

• New patterns can be added via “training”

• Write a canonical program that contains the necessary and sufficient code components comprising the pattern ( and little else )

Page 104: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

SPQR tools will extract the facts into r-calculusHand tune the definition and test

Page 105: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Other forms of Variance

• Vary by type… inheritance

• Get the photo of whiteboard

• MDL explains patterns existence

Page 106: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Are Patterns Arbitrary?

• Why are patterns the way they are?

• MDL explains it… GoF are minimal in several measures that make sense for software complexity

• Software Architecture

Page 107: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Advantages of SPQR

• Near zero learning curve

• Adding to existing tool chain, not replacing it

• Not adding to workload of engineer

• Analogous to a spelling checker or execution profiler

• Theorem prover + r-calculus + transitivity (isotopes) gives searching over infinite design space using finite number

of definitions• New way to teach OO concepts and design

Page 108: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012
Page 109: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Design Space 2.6

Page 110: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Design Space 2.7

Page 111: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Design Space 2.8

Page 112: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Design Space 3D

Page 113: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

2D Projection of 3D SpaceMethod similarity fixed at

“similar”

Page 114: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

2D Projection of 3D SpaceMethod similarity fixed at

“dissimilar”

Page 115: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Recursion

Page 116: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Deputized Recursion

Page 117: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

DecoratorUML collaboration diagram

Page 118: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Strategyrole-annotated class diagram

Page 119: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Big Class Diagram

Page 120: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Decorator towards PIN

Page 121: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Decoratortowards PIN

Page 122: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Fulfill Method

Page 123: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Fulfill Method

Page 124: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Decorator (reprise)

Page 125: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Decorator base

Page 126: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Up a level…

Page 127: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

4.13.a

Page 128: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

4.13.b

Page 129: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

4.14

Page 130: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

4.15

Page 131: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

4.16

Page 132: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

4.17

Page 133: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

4.18

Page 134: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

4.19

Page 135: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

4.20

Page 136: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

4.21

Page 137: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

4.22

Page 138: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

4.23

Page 139: Jason Smith The Software Revolution, Inc. based on UNC PhD work, and “Elemental Design Patterns”, Addison-Wesley, 2012

Pattern