introduction to the flow factory design pattern
DESCRIPTION
If you're an experienced Apex developer and have wondered if there?s a better way to manage multi-step logic that involves both DML and callouts to external systems, this session is for you. The Flow Factory is a stateful design pattern that provides structure for processing flowchart style business logic in Apex. This begins with flow-specific inputs and results in a chronological record of executed steps to iterate for final processing. It has roots in both the State pattern and the Factory Method and is particularly useful for managing more than one callout in a single execution context.TRANSCRIPT
Introduction to the Flow Factory Design Pattern Developers Track
Adam Purkiss, MondayCall Solutions, Principal [email protected]
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
Update Salesforce
Update profile
Create profile
YN
In international registry?
Course Complete
Done
Y
N
Welcome email
In US registry?
Update profile
The problem grew
Update Salesforce
Update profile
Create profile
YN
In international registry?
Course Complete
Done
Y
N
Welcome email
In rewards program?
Add rewards points
In US registry?
Rewards email
Update profile
Y
N
Callout constraints
Deferring DML and email
Update Salesforce
Update profile
Create profile
YN
In international registry?
Course Complete
Done
Y
N
Welcome email
In rewards program?
Add rewards points
In US registry?
Rewards email
Update profile
Y
N
▪ 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()
The Flow Factory
Flow of the flow
Pass 1
Step 1▪Final processing
Pass 2 (DML)
Step 1▪Add self to list▪Process work▪Create next
Step 2▪…▪…▪…
Step 3▪…▪...▪…
Step 4▪...▪…▪Return null
Step 2▪…
Step 3▪…
Step 4▪…
Flow of the flow
Pass 1
Step 1finish()
Pass 2 (DML)
Step 1processStep()
Step 2processStep()
Step 3processStep()
Step 4processStep()
Step 2finish()
Step 3finish()
Step 4finish()
Logical interfaces▪ FlowStep (create / process / finish)▪ FlowState (inputs / aggregate)▪ FlowParent (process loop / finish loop)
1) Next step / step name2) Process step / finish3) Create instance
FlowStep
1
3
2
▪ Create dynamic instance
FlowStep variation
1) Steps aggregate in parent list2) Flow-specific inputs in subclass
FlowState
2
1
1) Provide initial step & state2) Main loop (pass 1)3) Finish loop (pass 2)
FlowParent
3
1
2
1) Subclass FlowParent / processWork()2) Invoke flow
Invoking a flow example
2
1
Pass 1
Update Salesforce
Update profile
Create profile
YN
In international registry?
Course Complete
Done
Y
N
Welcome email
In rewards program?
Add rewards points
In US registry?
Rewards email
Update profile
Y
N
processStep()Nothing
processStep()Callout
processStep()Callout
processStep()Callout
Pass 2
Update Salesforce
Update profile
Create profile
YN
In international registry?
Course Complete
Done
Y
N
Welcome email
In rewards program?
Add rewards points
In US registry?
Rewards email
Update profile
Y
N
finish()Update Salesforce (DML)
finish()Save callout responseSend email
finish()Save callout responseSend email
finish()Save callout response
Demo
ResourcesGitHub:▪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-design-patterns-part2
Adam Purkiss
Principal Architect,@apurkiss