2

Could Thread class in Java be an example of state pattern. As state pattern says "encapsulate state based behavior and delegate behavior to the current state".

However in thread class all the states are defined as enum not as encapsulated state with their behaviour so it should not be. Please clarify

jaco0646
  • 15,303
  • 7
  • 59
  • 83

2 Answers2

3

No, As per state pattern, on the bases of state, substituting classes change the behavior of the object. But in the thread we do not define any substituting class corresponding to the states New, Runnable, Running and Dead. So thread is not an example of State pattern.

0

The Thread is an example of the command pattern. Due to the interface with an action/execute method, Many get the strategy and command patterns mixed... so your intuition wasn't too off.

The main difference here is that thread is more focused on executing and not holding the caller responsible for the handling of the process. The state pattern is more focused on executing a algorithm dynamically, but it also handles the operation.

Java API Pattern Examples

Thread Pools Command Pattern

Community
  • 1
  • 1
Pumphouse
  • 2,013
  • 17
  • 26