the stateful elephpant
TRANSCRIPT
![Page 1: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/1.jpg)
THE STATEFUL ELEPHPANT
RICK KUIPERS / @RSKUIPERS
@PHPSTORMTIPS
![Page 2: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/2.jpg)
STATE
![Page 3: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/3.jpg)
CODE EXAMPLE
![Page 4: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/4.jpg)
![Page 5: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/5.jpg)
![Page 6: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/6.jpg)
THE STATE PATTERN
![Page 7: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/7.jpg)
CODE EXAMPLE
![Page 8: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/8.jpg)
![Page 9: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/9.jpg)
![Page 10: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/10.jpg)
THE STATE STRATEGY PATTERN?
![Page 11: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/11.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/12.jpg)
WHY SHOULD WE CARE?
![Page 13: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/13.jpg)
CHECKOUT
PAYMENTDETAILS REVIEW
![Page 14: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/14.jpg)
CODE EXAMPLE
![Page 15: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/15.jpg)
![Page 16: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/16.jpg)
![Page 17: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/17.jpg)
PROS
! Independent blocks of logic
! Easy to add new states without touching existing code
! Can easily be coordinated
![Page 18: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/18.jpg)
THE STATE MACHINE
![Page 19: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/19.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/20.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/21.jpg)
STATUSES
PENDINGNEW COMPLETED CANCELED REFUNDED
![Page 22: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/22.jpg)
![Page 23: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/23.jpg)
![Page 24: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/24.jpg)
CODE EXAMPLE
![Page 25: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/25.jpg)
PROS
! Our transitions are set in stone
! Centralised approach makes callbacks easy
! Good overview of all possible transitions
![Page 26: The Stateful ElePHPant](https://reader031.vdocuments.site/reader031/viewer/2022021813/58810da91a28ab22368b68f3/html5/thumbnails/26.jpg)
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