Say I have a class, User:
public class User {
private String name;
private Integer age;
private Double cash;
// Getters and setters...
}
Some magic system spits out data for all Users and my system picks it up. Some employees need to report on this data, but some of it is known to be incorrect. Since the source system is magic, nobody can mess with the user data in it, so they need to use my system to update this data so that it is correct in the report. Thus, I want to make a class that’s something like the following:
public class UserUpdate {
private UserPropertyEnum property;
private Something newValue;
private Date dateAdded;
// Getters and setters...
}
So the problem lies in that I don’t know the type of the field I’m changing. I suppose I could make newValue of type Object, and cast based on the value of the property enum…
Anyway, is there a good design pattern that solves this? And if there is, how is it implemented in the data layer (I’m using a relational database)?
(Please refrain from “you’re unchangeable magic system is stupid… don’t store data that’s known to be incorrect” posts as I’m aware of this and am completely powerless to do anything about it.)
Thanks!
In C#, I would use generics. You can do something like:
And then create an update, say, for Salary using:
I’m assuming your updates are hard coded into some sort of mechanism that “patches” your magic data, so the types of each update would be known. I believe generics work the same way in Java, but you’ll have to confirm.