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.

7 Answers 7


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.

  • 3
    "anemic domain models are considered an antipattern" - by Martin Fowler who, in PEAA, admits that he knows people who've worked that way for decades "very successfully". Thus, I suggest, less an anti-pattern and more a not-how-Fowler-likes-to-work. Nonetheless, good answer, +1.
    – pdr
    Feb 2, 2013 at 15:42
  • 4
    @pdr: Fowler may have coined the term, but he is by no means the only one who considers them an antipattern. I don't think anyone who's actually understood OO thinks it's a good idea to keep domain-specific logic out of the domain model. Feb 2, 2013 at 21:15
  • Thanks for the term correction, DTO was the word I was looking for last night but couldn't remember for the life of me. By changing the name around, things made more sense design wise. Feb 3, 2013 at 6:34
  • @pdr I think the best way to put it is that it's an antipattern in the context of OO programming. There are many other paradigms where it's perfectly fine, and you could even successfully work that way in an OO language (it just wouldn't be particularly object-oriented).
    – Daniel B
    Feb 3, 2013 at 12:54
  • 3
    @MichaelBorgwardt: I agree, he's not alone in that. But neither are people who disagree. If you'd said "anemic domain models are considered by some to be an anti-pattern," I'd have said nothing. Frankly, I think your whole answer would be better without the last paragraph anyway. The first half would have made a better comment; the second half offers nothing to this question.
    – pdr
    Feb 3, 2013 at 13:45

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.

  • I think the contract aspect is key in this discussion. Most people think of interfaces as only being necessary for polymorphism but a DTO interface is a great way to ensure your API implementation matches your API definition (i.e. OpenAPI spec). Auto-generating the interfaces from the spec is very easy so you're not really doing double work.
    – TastyWheat
    May 16, 2020 at 16:06

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.

  • 1
    Interfaces haven't been necessary for using mocking frameworks in ages. Feb 2, 2013 at 21:13
  • Sounds like you're advocating monkeypatching. I've been down that road with Moles in C# and it's not pretty. Better to pass in your collaborators if possible.
    – jhewlett
    Feb 2, 2013 at 22:02
  • 1
    Though the two mocking frameworks I use Mockito and easyMock cannot mock final classes when making them immutable. Feb 9, 2015 at 20:57

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.