Introduction to the Flow Factory
Design Pattern
Developers Track
Adam Purkiss, MondayCall Solutions, Principal Architect
@apurkiss
adampurkiss.com
Learning the hard way…

ENTER COMMAND: GO NORTH

■
Learning the hard way…

ENTER COMMAND:

■
Object-oriented design patterns
▪ Program to an interface, not an implementation
▪

Strive for loosely-coupled designs

▪

Encapsulate what varies

▪

Remember the open-closed principle
Object-oriented design patterns
Requirements gathering
The problem
Course
Complete
In international
registry?

In US registry?

Update
Salesforce

N
Create
profile

Y
Update
profile

N

Y

Update profile

Done
Welcome
email
The problem grew
Course
Complete
In rewards
program?

In US registry?

Update
Salesforce

N
Create
profile

Y
Update
profile

Y
Add rewards
points

N

In international
registry?

N

Y

Update profile

Done
Welcome
email

Rewards
email
Callout constraints
Deferring DML and email
Course
Complete
In rewards
program?

In US registry?

Update
Salesforce

N
Create
profile

Y
Update
profile

Y
Add rewards
points

N

In international
registry?

N

Y

Update profile

Done
Welcome
email

Rewards
email
The Flow Factory
▪ Flow-specific inputs
▪ Executed steps collection
▪ Steps create & return next steps
▪

Inversion of Control

▪ Two pass execution
▪

Pass 1: Step.processStep()

▪

Pass 2: Step.finish()
Flow of the flow
Pass 1
Step 1

Step 2

Step 3

Step 4

▪Add self to list
▪Process work
▪Create next

▪…
▪…
▪…

▪…
▪...
▪…

▪...
▪…
▪Return null

Step 1

Step 2

Step 3

Step 4

▪Final processing

▪…

▪…

▪…

Pass 2 (DML)
Flow of the flow
Pass 1
Step 1

Step 2

Step 3

Step 4

processStep()

processStep()

processStep()

processStep()

Step 1

Step 2

Step 3

Step 4

finish()

finish()

finish()

finish()

Pass 2 (DML)
Logical interfaces
▪ FlowStep (create / process / finish)
▪ FlowState (inputs / aggregate)
▪ FlowParent (process loop / finish loop)
FlowStep
1) Next step / step name
2) Process step / finish
3) Create instance
1
2
3
FlowStep variation
▪ Create dynamic instance
FlowState
1) Steps aggregate in parent list
2) Flow-specific inputs in subclass
1

2
FlowParent
1) Provide initial step & state
2) Main loop (pass 1)
3) Finish loop (pass 2)
1
2

3
Invoking a flow example
1) Subclass FlowParent / processWork()
2) Invoke flow
1

2
Pass 1
processStep()
Course
Nothing
Complete

Update
Salesforce

In rewards
program?
processStep()
Callout

In US registry?

processStep()
Callout

N
Create
profile

Y
Update
profile

Y
Add rewards
points

N

In international
registry?
processStep()

N

Callout

Y

Update profile

Done
Welcome
email

Rewards
email
Pass 2
finish()
Course
Update Salesforce
(DML)Complete
In US registry?

Update
Salesforce

Save callout response
Send email

N

In international
registry?
finish()

N

Save callout response

N

finish()

In rewards
program?

Create
profile

Y
Update
profile

Y
Add rewards
finish() points
Save callout response
Send email

Y

Update profile

Done
Welcome
email

Rewards
email
Demo
Resources
GitHub:
▪https://github.com/apurkiss/course-complete

Developerforce Wiki:
▪http://wiki.developerforce.com/page/The_Flow_Factory_Design_Pattern

Pluralsight: Force.com Design Patterns - Part 2
▪http://pluralsight.com/training/Courses/TableOfContents/forcedotcom-designpatterns-part2
Adam Purkiss
Principal Architect,
@apurkiss
Introduction to The Flow Factory Design Pattern

Introduction to The Flow Factory Design Pattern