paytonpy
paytonpy

Reputation: 291

preserve backward compatibility without a separate class

Let's say there is a class called Animal which receives a request including animal's properties (type, color, size) and then it serializes and stores that in database. Over time, animal's properties change. Something is added and now we have location in addition.

There will be a version flag that can be used to branch out requests at any time.

How would you tweak animal class that it can handle the old requests as well as new ones? Is there any design patter for this?

Upvotes: 1

Views: 221

Answers (2)

Plamen G
Plamen G

Reputation: 4759

I don't think you need a specific design pattern to achieve this. Here are two approaches you can try and assess which one fits your case better:

  • Overload the current Request method with additional argument location like Animal::Request(type,color,size,location) so your class can handle both. More on overloading here.
  • Add a default location argument to your existing Request method and have it treat old type,color,size calls as type,color,size,'' calls, for example. More on default arguments here.

Upvotes: 2

Rafał Łużyński
Rafał Łużyński

Reputation: 7322

Sometimes you just can't make something backwards compatibile, it depends on your domain logic (rules).

If new property like "location" has some default value and you are fine with this, then you can work with old requests, but if that value is needed for your app to work properly, then old requests will be invalid.

Upvotes: 2

Related Questions