Saturday, 25 January 2014

State Design pattern - Real time example [TV Remote]

Master Practical Java Architecture & Design Patterns!

Subscribe to Ram N Java for clean coding tutorials, GoF design patterns, and real-world system implementations!

🔔 Subscribe to Ram N Java

The Real-Time TV Remote Problem

Consider an everyday device we all use: a TV Remote Controller. The remote has buttons such as power, volume up, volume down, and channel change. However, how the remote responds to pressing a button depends entirely on the current operating state of the television:

  • If the TV is Off, pressing the power button turns it On, while volume and channel buttons have no effect.
  • If the TV is On, pressing the power button turns it Off and places it into standby mode, while volume and channel buttons adjust audio and change broadcasts.

In traditional procedural code, you would implement this using conditional flags and nested if-else or switch-case statements. As more states are added—such as Mute, Input/Source Selection, or Menu Navigation—the conditional logic quickly becomes bloated, fragile, and hard to maintain.

How the State Pattern Solves the TV Remote Scenario

The State Design Pattern tackles this challenge by encapsulating state-specific behavior into dedicated classes, allowing the remote context to alter its behavior dynamically at runtime:

  • State Interface: Outlines operations common to states, such as doAction(TVContext context) or button presses.
  • Concrete State Classes: Classes like TVStartState and TVStopState define the behavior valid for that specific state and transition the TV to the next state when actions occur.
  • Context (TV Remote / Context): Maintains a reference to the active state instance and delegates button actions directly to that state object, keeping the client code clean and completely unaware of internal state checks.

Key Architectural Benefits

Applying the State pattern to this real-world scenario offers significant software engineering advantages:

  • Eliminates Bulky Conditionals: Replaces messy if-else blocks with clean, polymorphic method calls.
  • Single Responsibility Principle: Keeps the code related to each individual TV state isolated in its own dedicated class.
  • Open/Closed Principle: Adding new states (e.g., MuteState, GamingModeState) requires adding new classes without modifying existing state handlers.

Related Tutorials from Ram N Java


Click here to watch in Youtube : https://www.youtube.com/watch?v=JM6abyZ0snc

Click the below Image to Enlarge
State Design pattern - Real time example [TV Remote]



















State Design pattern - Real time example [TV Remote]




















See also:

  • State Design pattern - Introduction
  • State Design pattern - Real time example [ATM]
  • State Design pattern - Real time example [Vending Machine]
  • State Design pattern - Real time example [Project]
  • State Design pattern - Class Diagram
  • State Design pattern - Implementation [ATM]
  • State Design pattern - Implementation [Vending Machine]
  • State Design pattern - Implementation [Project]
  • State Design pattern - Implementation [LED TV]
  • State Design pattern - Keypoints
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Tutorials