What is the advantage of distributing the interaction algorithm in the RoleMethods, as suggested by DCI, instead of centralizing it in a Context method?

It is really a design decision. It is always possible to centralize the interaction code by rewriting the RoleMethods to become instance methods in the Context. The Roles are instance variables in the Context. The method names may have to be changed to avoid name clashes, and any occurrence of 'self' in the code must be replaced by the corresponding role name.

However, a difference between that kind of procedure orientation and object orientation is that in the former, we ask: “What happens?” In the latter, we ask: “Who does what?” Even in a simple example, a reader looses the “who” and thereby important locality context that is essential for building a mental model of the algorithm.

A centralized architecture may be OK in simple cases, but the decentralized (DCI) architecture leads to more sophisticated mental model and code. The natural model is for Roles to act like actors in a play, interacting with each other to carry out the work of the system.

Most of the time, putting the algorithm in the Context is global procedural thinking: an all-knowing algorithm orchestrating conscribed objects to perform low-level operations on the behalf of the procedure. You can of course do that in DCI and of course can do it through generic interfaces, but it's not DCI.

So to illustrate, we have the current “OO” way of creating object communication, passing messages between objects wherever and whenever it is needed:

This approach is too chaotic, and it doesn't help that there is no separation of data and behavior in the current OO paradigm, or that polymorphism often makes it very hard to know where the program execution will move next. Reasoning about system behavior and functionality is very hard.

Then we have procedural thinking, or as it can be implemented in OO-programming, the mediator pattern:

This approach actually prevents object communication, leading to a complex all-knowing algorithm. It's too rigid for responding quickly to system changes, so stepping back to Pascal or Fortran-style isn't a good solution either.

DCI strikes a middle road, distributed logic under full control, functionality encapsulated in a Context:

Trygve explains the differences between the three:

Is DCI a procedure oriented paradigm in disguise? My answer is no. Both state and behavior is here distributed among the objects. I see DCI as even more object oriented than regular OO. In DCI, we work with networks of collaborating objects; in regular OO, we work with classes and can only see one object at the time.1)

Once in a while though, this Context-centric algorithm could have a place. Taking the age-old example of drawing a shape on a window, where each object comes from its corresponding class hierarchy. If the draw algorithm doesn't easily decompose in a way that fits naturally in either class or in any corresponding roles, it belongs in the Context. This is the exceptional case, however.

what_is_the_advantage_of_distributing_the_interaction_algorithm_in_the_rolemethods_as_suggested_by_dci_instead_of_centralizing_it_in_a_context_method.txt · Last modified: 2014/10/21 23:36 by gazoot