Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Good Intentions II: Enemy of the State

Good Intentions II: Enemy of the State

Originally presented at 360iDev in Denver, CO on August 19, 2015

In a world where ViewControllers shoulder the burden of managing ever-changing vagarities of application state logic and complex navigation stacks, one design pattern emerges from the past to lead us toward a better, brighter, and lighter future of app development.

In this sequel to last year’s blockbuster presentation, “Good Intentions: Building Better ViewControllers”, we will revive a venerable programming technique for explicitly defining application behaviors and thereby reducing the responsibilities of our much maligned ViewControllers.

Jay Thrash

August 19, 2015
Tweet

More Decks by Jay Thrash

Other Decks in Programming

Transcript

  1. Good Intensions 2 Enemy of the State Jay Thrash @jaythrash

    360iDev, Denver August 2015 Intentions
  2. Good Intentions 2 Enemy of the State Jay Thrash @jaythrash

    360iDev, Denver August 2015 “relax”
  3. Jay Thrash @jaythrash 360iDev, Denver August 2014 A Path to

    Building Better ViewControllers Good Intentions
  4. Jay Thrash @jaythrash 360iDev, Denver August 2014 A Path to

    Building Better ViewControllers Good Intentions Stores Techniques 3 Delegate(v) the DataSources Intentions
  5. BOOL ENUM & Managing state ✓Easy to start ‣Minimal states

    ‣Quick to Conflict ✓No Conflicts ‣From Any, to Any ‣No Rules
  6. Managing state BOOL ENUM & ✓Easy to start ‣Minimal states

    ‣Quick to Conflict ✓No Conflicts ‣From Any, to Any ‣No Rules TOO AMBIGUOUS
  7. “A mathematical model of logic. A state machine can be

    in one of a finite number of states. The machine is in only one state at a time; the state it is in at any given time is called the current state.” State Machines abstract
  8. State Machines ✓ Simple Configuration ✓ Transition Logic “DSL” ✓

    Conforms to SRP ✓ Facilitates Automated Testing practical
  9. State Machines NSString * const AAPLLoadStateInitial = @"Initial"; NSString *

    const AAPLLoadStateLoadingContent = @"LoadingState"; NSString * const AAPLLoadStateRefreshingContent = @"RefreshingState"; NSString * const AAPLLoadStateContentLoaded = @"LoadedState"; NSString * const AAPLLoadStateNoContent = @"NoContentState"; NSString * const AAPLLoadStateError = @"ErrorState"; ✓Simple Configuration practical States
  10. State Machines self.validTransitions = @{ AAPLLoadStateInitial : @[AAPLLoadStateLoadingContent], AAPLLoadStateLoadingContent :

    @[AAPLLoadStateContentLoaded, AAPLLoadStateNoContent, AAPLLoadStateError], AAPLLoadStateRefreshingContent : @[AAPLLoadStateContentLoaded, AAPLLoadStateNoContent, AAPLLoadStateError], AAPLLoadStateContentLoaded : @[AAPLLoadStateRefreshingContent, AAPLLoadStateNoContent, AAPLLoadStateError], AAPLLoadStateNoContent : @[AAPLLoadStateRefreshingContent, AAPLLoadStateContentLoaded, AAPLLoadStateError], AAPLLoadStateError : @[AAPLLoadStateLoadingContent, AAPLLoadStateRefreshingContent, AAPLLoadStateNoContent, AAPLLoadStateContentLoaded] }; ✓ Simple Configuration practical Transitions
  11. State Machines ✓ Transition Logic “DSL” practical // NSString *

    const AAPLLoadStateLoadingContent=@“LoadingState”; I (void)somethingHappened { self.stateMachine.currentState = AAPLLoadStateLoadingContent; }
  12. State Machines ✓ Transition Logic “DSL” practical // NSString *

    const AAPLLoadStateLoadingContent = @“LoadingState”; I (void)didEnterLoadingState { [self updatePlaceholder:self.placeholderView]; } I (void)didExitLoadingState { [self thatActionOnLeavingLoadingState]; }
  13. State Machines ✓ Conforms to SRP practical AAPLStateMachine AAPLStateMachine Delegate

    MyClass AAPLStateMachine Delegate •Configure & Conform •Request & React •Validate •Initiate
  14. State Machines Whom Initial Whom Valid Direct Search Direct Search

    Valid Direct Chosen When Indirect Indirect Valid Direct Reason Direct Reason Valid Indirect Reason Indirect Reason Valid Applied: branching navigation
  15. State Machines Whom Initial Whom Valid Direct Search Direct Search

    Valid Direct Chosen When Indirect Indirect Valid Direct Reason Direct Reason Valid Indirect Reason Indirect Reason Valid Applied: branching navigation
  16. Next Steps Topics to Google ➡ WWDC 2014, Session 232


    Advanced UI with CollectionViews ➡ Soroush Khanlou 
 khanlou.com