Login :
Password :

Design patterns

Hidden switch statement

Static diagram :

Static diagram

Strong points :

  1. 1. Extensibility
    1. KO 1.1 States' protocol factorization
    2. KO 1.2 Addition or removal of a state does not need code modification
  2. 2. Simplified management
    1. OK 2.1 Possibility of state modification at execution time without deletion
    2. KO 2.2 State behavior decoupling

Spoiled pattern instantiations on different problems

Problem 1 :

Problem wording :
Design a VOD platform. The platform provides its customers with movies in three categories: Child, Normal and New. A movie is in the "New" category for a few weeks, then moves on to one of the other categories. The price of the movies depends on the category. The system may evolve so that the Horror category will be added.
Optimal solution :

Static diagram :

Static diagram

Thanks to the inheritance links, we are able to distinct the movies to its states. In these case, the addition of a new state does not imply code modification.

Alternative solution :

Static diagram :

Static diagram

The VOD class manages its state thanks to an enumeration. In doing so, the solution imposes a ``switch'' statement, and so, the category changing is possible. The problem of this solution concerns the extensibility. Indeed, if a new category is added, the VOD class must be modified to manage the new type.

Les têtes de mule
Cédric BOUHOURS
Cédric BOUHOURS
Cédric BOUHOURS