1

I am very confused about the responsibility-driven design concept. Mainly because of ever so slightly changing definitions depending on the source.

Quoting BlueJ (the book I am learning that teaches Java):

Responsibility-driven design expresses the idea that each class should be responsible for handling its own data. Often, when we need to add some new functionality to an application, we need to ask ourselves in which class we should add a method to implement this new function. Which class should be responsible for the task? The answer is that the class that is responsible for storing some data should also be responsible for manipulating it.

Later, in a "concept box" in the BlueJ book:

Reponsibility-driven design is the process of designing classes by assigning well-defined responsibilites to each class. This process can be used to determine which class should implement which part of an application function.

This second definition confuses me, as I don't see how that correlates to the first "definition"; the one saying that "it expresses the idea that each class should be responsible for handling its own data".

Will someone please shed some light on the concept of responsibility-driven design?

Community
  • 1
  • 1
Sebastian Nielsen
  • 3,835
  • 5
  • 27
  • 43
  • I added a "Java" tag in case the concept is different depending on the language, in which case I am interested in the concept in regard to Java. – Sebastian Nielsen Jan 04 '20 at 20:04
  • 3
    This might be a better fit for [softwareengineering.stackexchange.com](https://softwareengineering.stackexchange.com/help/on-topic). – ggorlen Jan 04 '20 at 20:06
  • 2
    Please don’t expect concepts to be well-defined, let alone have only one definition and an unambiguous one. For my part I tend to understand reponsibility-driven design (though not a term I am using actively) along the ideas of the second quotation, but I find the explanation in there vague, I still think the first quote explains it better. – Ole V.V. Jan 04 '20 at 20:26
  • @OleV.V. Another thing that I am a little uncertain about is how do you choose the "right/optimal" responsibilities for each class? Is that where the first definition comes into question, the one saying that if a class stores something, it should also bear the responsibility of handling it? – Sebastian Nielsen Jan 04 '20 at 20:47

2 Answers2

3

Disclaimer: I haven’t learned Reponsibility-driven design formally, though I think that the students that I have taught, will testify that I have talked a great deal about responsibility in class design. Instead this answer draws on 25 years of experience in object-oriented design, most of it in Java.

Responsibility-driven versus object-oriented

I do to a large extent think in responsibility when designing a class (so I guess that I am doing responisibility-driven design as defined in your quotes). Ideally I think responsibility more in terms of what the instances of the class (or the class itself) can do and only secondly in terms of what data they store. This seems to agree with your second definition. In some situations I can’t uphold this ideal, though.

Putting the manipulation of data in the class where the data is (as in your first quote), I consider this a general object-oriented principle (maybe depending on which object-oriented school you subscribe to). Often expressed keep data and code together. I really don’t think that this is something specific to responsibility-driven design (but of course has its place there too).

An example. Say that a Person object should have the responsibility of calculating a person’s age. I would consider this sound responsibility-driven design. To do this the Person object needs the person’s birth date. To me object-oriented design would put the birth date in the Person object so we have code and data together. This could be in contrast to for example keeping a list of birth dates. In my understanding assigning the responsibility of storing the birth dates to a separate birthday list might still be responsibility-driven, but it would not be object-oriented. This means, I don’t really agree with your first definition.

So I understand your confusion. I agree with you that the two definitions don’t say the same thing.

I am creating a distinction that in real life isn’t that clear, though. In my daily work object-oriented and responsibility-driven design go nicely hand in hand, and I don’t think consciously about the difference.

Another quote - on object-orientation

Encapsulation is a mechanism of wrapping the data (instance variables) and code acting on the data (methods) together as a single unit like a Class.

From OOP: Everything you need to know about Object Oriented Programming, where encapsulation is considered the first of 4 object-oriented programming concepts.

Examples of RDD from the Java standard library

I think that the following quotes from the documentation express pretty clear responsibility-driven thoughts behind the design of those interfaces and classes:

  • To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. (Serializable)
  • In practice, a button's UI takes the responsibility of calling methods on its model to manage the state, as detailed below: (ButtonModel)
  • The PersistenceDelegate class takes the responsibility for expressing the state of an instance of a given class in terms of the methods in the class's public API. Instead of associating the responsibility of persistence with the class itself as is done, for example, by the readObject and writeObject methods used by the ObjectOutputStream, streams like the XMLEncoder which use this delegation model can have their behavior controlled independently of the classes themselves. (PersistenceDelegate)
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Let's say we assign each class an illogical (though well-defined) responsibility. For instance, `Person` should be responsible for updating the weather (an illogical responsibility for `Person`). Now, the definition (my second quote) doesn't say anything about the responsibilities having to be logical; thus, assigning those illogical responsibilities to each class must still cohere to the concept of responsibility-driven design. Wouldn't you agree? – Sebastian Nielsen Jan 05 '20 at 09:50
  • 1
    There are hardly any design guidelines that will prevent you from making illogical designs if you insist. Yes, i agree with you. „sat på spidsen” (don’t know the best English term) – Ole V.V. Jan 05 '20 at 09:57
  • I agree with you. But, now, do you acknowledge that whenever you create a class and define it a method, you are basically assigning that class a responsibility whether you like it or not, namely, the responsibility of performing that action (method) -- (I would very much like to know if you agree with this). Thus (if you agree with that), you are basically always adhering to responsibility-driven design (RDD). But if you're always adhering to RDD, then there is no point in actively thinking about it when programming. – Sebastian Nielsen Jan 05 '20 at 10:14
  • Still following your second definition, RDD is a process, a way of getting to a result. It is not the result itself. So yes, when I create a method and expect it to behave in some way, I am implicitly assigning responsibility (if I didn’t care about how it behaves, I am not, but then I wouldn’t care to create the method). And no, this alone does not make my process responsibility-driven. It only gets RDD when I actively or consciously *think* responsibility before or when creating the method. – Ole V.V. Jan 05 '20 at 10:53
  • You may also say that RDD may include steps that don’t involve concrete methods. From the vaguest “a `Person` object has the responsibility of doing stuff related to persons” to the pretty concrete “a `Person` object has the responsibility of telling a person’s age in years”, which still doesn’t include a concrete method design, are RDD statements. – Ole V.V. Jan 05 '20 at 15:51
1

Responsibility-Driven Design is pretty well-defined in fact. This is an object oriented design methodology that was introduced by Rebecca Wirfs-Brock and Brian Wilkerson in their book "Object Design: Roles, Responsibilities and Collaborations" published in 2003. I found this book enlightening when I read it about 10 years ago. There's an overview of RDD available on Rebecca Wirfs-Brock's site. Though I am not sure if BlueJ authors meant this particular methodology or they coined the term on their own.

Rorick
  • 8,857
  • 3
  • 32
  • 37