vendredi 24 février 2017

Do I need a design pattern here?

I have a rather theoretical question.

For example I have an class of objects - like Table. And this table could be colored with different colors - red, blue and yellow. If the table was not colored before, the result is clear. Otherwise the following rules are in play:

red + yellow --> orange
blue + yellow --> green
red + blue --> purple
red + blue + yellow --> brown

What is the nicest and the most elegant way to implement this? Is there any design patter for such a case? My variant may be too straightforward:

public class Table{

private boolean isRed;
private boolean isBlue;
private boolean isYellow;

     public String colorRed(){
         isRed = true;
         if (isBlue && isYellow) 
               return "brown";
         if (isBlue)
               return "purple";
         if (isYellow)
               return "orange";
         return "red";
     }
     //... the same for other colors
}

Aucun commentaire:

Enregistrer un commentaire