introduction to the flow factory design pattern

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

Upload: salesforce-developers

Post on 08-May-2015

494 views

Category:

Technology


0 download

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

Page 1: Introduction to The Flow Factory Design Pattern

Introduction to the Flow Factory Design Pattern Developers Track

Adam Purkiss, MondayCall Solutions, Principal [email protected]

Page 2: Introduction to The Flow Factory Design Pattern

Learning the hard way…

ENTER COMMAND: GO NORTH■

Page 3: Introduction to The Flow Factory Design Pattern

Learning the hard way…

ENTER COMMAND: ■

Page 4: Introduction to The Flow Factory Design Pattern

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

▪ Strive for loosely-coupled designs▪ Encapsulate what varies▪ Remember the open-closed principle

Page 5: Introduction to The Flow Factory Design Pattern

Object-oriented design patterns

Page 6: Introduction to The Flow Factory Design Pattern

Requirements gathering

Page 7: Introduction to The Flow Factory Design Pattern

The problem

Update Salesforce

Update profile

Create profile

YN

In international registry?

Course Complete

Done

Y

N

Welcome email

In US registry?

Update profile

Page 8: Introduction to The Flow Factory Design Pattern

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

Page 9: Introduction to The Flow Factory Design Pattern

Callout constraints

Page 10: Introduction to The Flow Factory Design Pattern

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

Page 11: Introduction to The Flow Factory Design Pattern

▪ 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

Page 12: Introduction to The Flow Factory Design Pattern

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▪…

Page 13: Introduction to The Flow Factory Design Pattern

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()

Page 14: Introduction to The Flow Factory Design Pattern

Logical interfaces▪ FlowStep (create / process / finish)▪ FlowState (inputs / aggregate)▪ FlowParent (process loop / finish loop)

Page 15: Introduction to The Flow Factory Design Pattern

1) Next step / step name2) Process step / finish3) Create instance

FlowStep

1

3

2

Page 16: Introduction to The Flow Factory Design Pattern

▪ Create dynamic instance

FlowStep variation

Page 17: Introduction to The Flow Factory Design Pattern

1) Steps aggregate in parent list2) Flow-specific inputs in subclass

FlowState

2

1

Page 18: Introduction to The Flow Factory Design Pattern

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

FlowParent

3

1

2

Page 19: Introduction to The Flow Factory Design Pattern

1) Subclass FlowParent / processWork()2) Invoke flow

Invoking a flow example

2

1

Page 20: Introduction to The Flow Factory Design Pattern

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

Page 21: Introduction to The Flow Factory Design Pattern

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

Page 22: Introduction to The Flow Factory Design Pattern

Demo

Page 24: Introduction to The Flow Factory Design Pattern

Adam Purkiss

Principal Architect,@apurkiss

Page 25: Introduction to The Flow Factory Design Pattern