Design Pattern Series

State Method

By: Chip Salim

TBD 2017

#2

Agenda

  1. Recollection from DP Session #1
  2. The State Pattern

Recollection

  • Session Series as re-freshers & discovery
    • Beginner, Mid-Level, and Advanced
    • Shared vocabs. to increase comm.

Recollection

  • Basics of Design Pattern
    • What it is and is not

Recollection

  • Start with a KISS and A PIE
    • Refactoring
    • Apply OOP

Recollection

Types

  • Creational, Behavioral, Structural, Concurrency

Recollection

Strategy Pattern

Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

State Pattern

Allows an object to alter its behavior when its internal state changes. The object will appear to change its class.

Relationships

Strategy Pattern

State Pattern

Relationships

Strategy Pattern

State Pattern

  • Flexible alternative to subclassing
  • The client usually specifies the strategy object to compose the Context
  • Policy PatternĀ 
  • A substitute to many if-then-else statements
  • The client interacts with the Context. The Context delegates its internal state through its subclasses

Context

Off

Locked

Unlocked

Display Main Screen

Display Login Screen

Password

Required

?

No

Yes

Password

Valid

?

No

Switch

Switch

Switch

Yes

Context

Off

Locked

Unlocked

Locked

Out

Display Main Screen

Display Login Screen

Password

Required

?

No

Yes

Password

Valid

?

No

3rd

Attempt

?

Yes

No

Switch

Switch

Switch

Yes

Summary

The State pattern allows an object to alter its internal state (behavior) via delegation at run-time

Characterized by a long list of conditional statements

The State and Strategy patterns share the same structure, the difference is in the intent.

Result in more numbers of classes in your design

Danke!

Next Topic by Scott Carroll

Made with Slides.com