vendredi 23 mars 2018

State Design Pattern Usage In Tennis Kata

I am solving the famous Tennis Kata Problem

I have a basic question around state design pattern. The programme needs to output the score for a tennis game.

Below enum represents the different scores a player in tennis game can have :

public enum Score{
Love,
Fifteen,
Thirty,
Fourty,
Deuce,
Advantage,
Game
}

Typical classes I am planning to use here are : TennisGame (main class for handling logic), Player (will maintain its own Score), ScoreCalculator (have Score manipulation logic based on Rules) The scores will be changed when an external event happens which indicates which user has scored the point.

The question here is - how the logic to track score changes should be handled ?

Very naive approach will be :

if(score == Love){
  score = Fifteen; //move score to Fifteen 
}else if(score == Fifteen){
  score = Thirty; //move score to Thirty
}

But this will lead to a lot of if else conditions. I am thinking to use a state pattern for this.

But the question here is , the context here (e.g: TennisGame class) which will encapsulate State won't be doing anything based on State changes.

It is just returning the score once a point is scored by a player.

Can anyone please advice what should be the correct design using State pattern here ?

Aucun commentaire:

Enregistrer un commentaire