27
THE STATEFUL ELEPHPANT RICK KUIPERS / @RSKUIPERS @PHPSTORMTIPS

The Stateful ElePHPant

Embed Size (px)

Citation preview

Page 1: The Stateful ElePHPant

THE STATEFUL ELEPHPANT

RICK KUIPERS / @RSKUIPERS

@PHPSTORMTIPS

Page 2: The Stateful ElePHPant

STATE

Page 3: The Stateful ElePHPant

CODE EXAMPLE

Page 4: The Stateful ElePHPant
Page 5: The Stateful ElePHPant
Page 6: The Stateful ElePHPant

THE STATE PATTERN

Page 7: The Stateful ElePHPant

CODE EXAMPLE

Page 8: The Stateful ElePHPant
Page 9: The Stateful ElePHPant
Page 10: The Stateful ElePHPant

THE STATE STRATEGY PATTERN?

Page 11: The Stateful ElePHPant

STRATEGY PATTERN

! Tries to achieve similar result for input

! Strategies can’t transition to otherstrategies

! Strategy processes the input and givesoutput back to the wrapper

STATE PATTERN

! Result can be completely different

! States can trigger transitions to otherstates

! State controls the behaviour of itscontext object (ElePHPant)

Page 12: The Stateful ElePHPant

WHY SHOULD WE CARE?

Page 13: The Stateful ElePHPant

CHECKOUT

PAYMENTDETAILS REVIEW

Page 14: The Stateful ElePHPant

CODE EXAMPLE

Page 15: The Stateful ElePHPant
Page 16: The Stateful ElePHPant
Page 17: The Stateful ElePHPant

PROS

! Independent blocks of logic

! Easy to add new states without touching existing code

! Can easily be coordinated

Page 18: The Stateful ElePHPant

THE STATE MACHINE

Page 19: The Stateful ElePHPant

THE STATE MACHINE

Ileft Iright Idown Iup Ispace

Sidling Srunning Srunning Scrouching Sjumping Ashoot

Srunning - - Scrouching Sjumping Ashoot

Sjumping - - - - -

Scrouching - - - - -

Page 20: The Stateful ElePHPant

THE STATE MACHINE

Input Current State Next State Output

le0 Sidling Srunning -

right Sidling Srunning -

up Sidling Sjumping -

space Sidling Sidling Ashoot

Page 21: The Stateful ElePHPant

STATUSES

PENDINGNEW COMPLETED CANCELED REFUNDED

Page 22: The Stateful ElePHPant
Page 23: The Stateful ElePHPant
Page 24: The Stateful ElePHPant

CODE EXAMPLE

Page 25: The Stateful ElePHPant

PROS

! Our transitions are set in stone

! Centralised approach makes callbacks easy

! Good overview of all possible transitions

Page 26: The Stateful ElePHPant

PACKAGES

DON’T REINVENT THE WHEEL

! https://github.com/winzou/state-machine

! https://github.com/Sylius/SyliusFlowBundle

! https://github.com/rskuipers/stateful-elephpant

! https://github.com/rskuipers/stateful-elephpant-talk

Page 27: The Stateful ElePHPant

4

HTTPS://JOIND.IN/TALK/DA5ED

FEEDBACK

@RSKUIPERS

RICK KUIPERS

QUESTIONS?

[email protected]