Is it a good idea or a bad idea to create an interface for data transfer objects? Presuming that the object is usually mutable.
Though my example is in Java, it should be applicable to any other language that has similar concepts.
interface DataTransferObject {
String getName();
void setName(String name);
}
class RealDataTransferObject implements DataTransferObject {
String name;
String getName() {
return name;
}
void setName(String name) {
this.name = name;
}
}
Of course, this is a simplified example, in real life there may be more fields.
The general answer is no, because you should never add code without having a specific, concrete reason for it, and there is no general reason for such an interface.
That being said, sometimes there can be a good reason. But in all cases I have seen, these interfaces were partial, covering only one or a few properties shared by multiple classes I wanted to use polymporphically without giving them a common superclass. Typical candidates are an Id
property to use in some sort of registry or a Name
property to display to the user. But it can be useful in any case where you want some code to handle everything that has an X – just create an XSource
interface that contains the getX
(and, only if required, the setX
) methods.
But a separate interface for every model class, containing all the properties? I can’t imagine a good reason to do that. A bad reason would be a badly designed framework that requires it; Entity EJBs did just that, if I remember correctly. Thankfully they were so bad they never gained much traction and are deprecated since EJB 3.0
Sidenote: please avoid using the term “value object” to describe Java beans with only trivial getters and setters – it conflicts with the more common definition of value object as something with no identity that is usually immutable. A better term would be DTO or model class – though in the latter case note that anemic domain models are considered an antipattern.
6
Interfaces define a contract between the classes implementing the interfaces and their clients. They are used as an abstraction mechanism so that clients can manipulate “stuff having a given behaviour”.
So the general answer to the question “should I create and use this interface?” is: Yes, if you can associate a (a single one) concept being semantically relevant for your clients.
For instance, Comparable is a good interface, because it explains that things can be compared thanks to one of their methods, and as a client I’m interested in dealing with comparable objects (e.g. for sorting them). A contrario, CoolStuff is not a good interface if you admit the cool objects don’t have a specific behaviour (in fact, you can imagine a software in which dealing with cool objects makes sense, because they have a common behaviour such as a beCool method).
In your particular case, I believe your interface is useless. Who will use it, how and when? You cannot create an interface for each of the mutable values. So ask yourself what is the relevant and interesting property behind your methods.
If what you want is dealing with objects having all their mutable values accessible through a couple of methods, have a look at the notion of Java bean and the way you can force your classes to adopt their conventions.
1
Creating interface is ok, but NOT the way your example works. You should remove the setter from the interface, and it’ll be ok:
interface ValueObject {
String getName();
};
This allows many different implementations of it, like name can be fetched from database… Setter should be in different interface.
As Michael said, don’t add an interface unless you have a specific need for it.
Testing is an example of a good reason. Though I prefer to use real collaborators if they are just “value objects” as you call them, for true unit test isolation you may need to create a fake object for testing, in which case an interface is quite helpful.
3
If you want this object to have some sort of fields validation in future, you should incapsulate them on early stages.
The ‘interface for value object’ is what i’m used to call an accessor.
I experienced different policies concerning accessors need. Some people advocate for when as much as possible, some other prohibit then to reduce the quantity of code to write.
Some rationnals for accessors (or direct value usage) are the following:
- Accessors allows changing later the way the value is stored
- Accessors allows adding access log when you want to debug your software (when adding a log call in a single method, you capture every value change)
- Accessors are more in line with object programming, every variable being encapsulated by methods
- Accessors reduces code expressivness (more SLOC for the same result)
- Accessors takes some CPU
I personally advocate to reduce the number of accessors, and to use them when you expect the setter (setName) to become more than a simple affectation later.
This kind of value object is pretty low level. I’d suggest pushing it in one of two directions: (1) make the value object immutable, that is, like a real value, or, (2) elevate the mutability to a higher level domain oriented business function, meaning we should expose interfaces in terms of domain relevant units of functionality.