lundi 16 mai 2016

Best practice when there is constructor change in java objects

I'm having a class in which many parameters are being added as per new api integration.

For example : Earlier I had a class in which there were 4 parameters :

Integer a;
String b;
Map<String, String> c;
List<Integer> e.

So the constructor was :

public SampleClass(Integer a, 
                   String b, 
                   Map<String, String> c,      
                   List<Integer> e) 
{
    this.a = a;
    this.b = b;
    this.c = c;
    this.e = e;
}

Several teams have integrated with my API using this constructor in their code. After sometime, there was a new parameter added to this class. i.e.

Double d;

So I added a new constructor :

public SampleClass(Integer a,
                   String b,
                   Map<String, String> c,
                   List<Integer> e,
                   Double d)
{
    this.a = a;
    this.b = b;
    this.c = c;
    this.e = e;
    this.d = d;
}

And I marked the previous constructor as deprecated. I did not remove the previous constructor because if removed, the client's code would break.

As the new parameters are getting added, I now have 5 constructors.

Is there a best practice on how the constructors should be deprecated/removed, so that this type of scenario does not occur.

Thanks

Aucun commentaire:

Enregistrer un commentaire