avatarItchimonji

Summary

The provided web content is a comprehensive guide on the State design pattern, illustrating its application, benefits, and real-world examples.

Abstract

The web content titled "State | Cheat Sheet" under the "Behavioral Pattern — Design Patterns Series" provides an in-depth look at the State pattern. It explains how an object's internal state change can affect its external behavior, with examples ranging from mobile phone screen states to traffic lights and video game controls. The pattern is suitable for scenarios where an object's behavior must change at runtime based on its state. The content highlights the pattern's ability to encapsulate state-specific behaviors, making state transitions clear and allowing for easy extension. It also touches on the concept of object composition and dependency inversion as related topics. A practical example using TypeScript demonstrates the implementation of the State pattern for a mobile phone's display states. The article concludes by emphasizing the importance of design patterns in software development for creating maintainable and readable code.

Opinions

  • The State pattern is highly beneficial for managing complex state-dependent behavior in a structured and maintainable way.
  • Design patterns, including the State pattern, are considered essential knowledge for developers to ensure consistent communication and system design.
  • The State pattern promotes the use of object composition over inheritance, aligning with the principles of object-oriented design such as dependency inversion.
  • The article suggests that the State pattern is particularly useful for extending and maintaining complex codebases, as it localizes state-specific behavior and simplifies the addition of new states.
  • The provided code example on GitHub is recommended as a practical resource for developers to understand and implement the State pattern in TypeScript.

State | Cheat Sheet

Behavioral Pattern — Design Patterns Series

Nowadays, a State can be found in a lot of application architectures: Web page state, database state (set of stored data), or stateful sets like MQTT — shortly explained, the entire content of its memory, e.g. all its variables, objects allocated, or open network sockets.

The pattern describes how changing the internal state affects the external behaviour, e.g. class changes.

Real-life examples

  • Screen states of the mobile phone: Lock Screen, Battery Low Screen, Unlocked Screen, …
  • Traffic lights in road traffic
  • Video games: Play, Pause, Load, Connect, …
  • Using the remote control to achieve various states on TV: on, off, mute, colour ratio, …
  • Ticket vending machine
  • Application Controller in Web pages: 1 controller handles different views as responses

Meaning

  • Adaptation of the behaviour of an object in case of an internal change of state (effect like class change)

Applicability (suitable if …)

  • The behaviour of an object depends on its state and its behaviours must be changed at runtime from this state
  • Operations depend on the state of the object (e.g. switch statement)

Assets and Drawbacks

  • Inventory of state-specific behaviours + distributes them to different states (state-specific code is in state subclass and thus allows easy extension)
  • Changes of state are clearly highlighted
  • State objects can be shared

Example

A good programmable example would be to change the mobile phone display from a locked view to an unlocked view.

Therefore, you need to start with the definition of the various screen states in an interface. A display can be locked, unlocked, and show a low energy icon when the battery is running low.

With an abstract class you can equip the interface to throw a runtime error if the method in the subclass is not implemented:

Now you can define the concrete classes for the different states:

At last, you need a mobile phone class that can switch between different states:

Conclusion

Design Patterns are an important resource and base knowledge for every developer — they are very helpful for solving programmatic problems, help with consistent communication with other developers about system design, and serve as a significant introduction into object composition (besides inheritance) and dependency inversion.

Behavioral Patterns deal with algorithms and assignment of responsibilities to objects. Furthermore, they describe mutual communication patterns and grasp complex program sequences. They are therefore very well suited for use in areas where complicated code needs to be made readable and maintainable. Extensions are, in consequence, very good and quickly implementable.

Code example on Github

Learn More

Resources

Design Patterns
State
Cheatsheet
Object Composition
Behavioral Patterns
Recommended from ReadMedium