Reputation: 291
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
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:
Animal::Request(type,color,size,location)
so your class can handle both. More on overloading here.type,color,size
calls as type,color,size,''
calls, for example. More on default arguments here.Upvotes: 2
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