lundi 14 mai 2018

How to overload a class

Initially I had a plan class which behaves like this:

class Plan {
     public enum Type {
         FOO
     }

     String checkFoo() {
        // some check for foo
     } 

     String doSomething() {
     }
} 

Now, I need to add another type called Bar.

class Plan {
     public enum Type {
         FOO, BAR
     }

     String checkFoo() {
        // some check for foo
     } 

     String checkBar() {
       // some check for bar
     }   

      String doSomething() {
     }
} 

Now, checkFoo only works for Foo but does not work for Bar and vice-versa. How is the best 'design pattern' to structure this ? Should we add checks in each method and throw exceptions ? Is there a common solutions ?

Aucun commentaire:

Enregistrer un commentaire