<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" gd:etag="W/&quot;DUYFQXw_fSp7ImA9WhRUGUs.&quot;"><id>tag:blogger.com,1999:blog-1114076417496162061</id><updated>2012-01-31T00:45:10.245+01:00</updated><title>Bartosz Blimke's Blog</title><subtitle type="html" /><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://www.bartoszblimke.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://www.bartoszblimke.com/" /><author><name>Bartosz Blimke</name><uri>http://www.blogger.com/profile/08810234693165301502</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="23" height="32" src="http://bp1.blogger.com/_sAj7ObFDriQ/R6ZLoYaQFuI/AAAAAAAAAAM/LDXAyvyUaSU/S220/masb2.jpg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/BartoszBlimke" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="bartoszblimke" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;Dk4MQXkyeCp7ImA9WhdWE0w.&quot;"><id>tag:blogger.com,1999:blog-1114076417496162061.post-3847400104010425233</id><published>2011-09-06T15:06:00.000+02:00</published><updated>2011-09-06T15:16:20.790+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-09-06T15:16:20.790+02:00</app:edited><title>DCI Is Not Against Inheritance</title><content type="html">&lt;p&gt;Recently I've been discussing an implementation, where object inheritance was used for one of models. The DCI paradigm was applied to implement this part of the system.
Some opinions were that adding inheritance breaks DCI conventions. To me this is misunderstanding of DCI. Inheritance applied to data model can not only co-exist with DCI, but actually can help to create a better domain.&lt;/p&gt;

&lt;h2 id="what_is_dci"&gt;What is DCI?&lt;/h2&gt;

&lt;p&gt;Data, Context, Interactions is a programming paradigm invented by &lt;a href="http://folk.uio.no/trygver/" target="_blank"&gt;Trygve Reenskaug&lt;/a&gt;.
DCI was created to improve code readability and maintainability, compared to the traditional OOP.
The basic idea behind DCI is to split Data (what object &lt;code&gt;is&lt;/code&gt;) from Interactions (what object &lt;code&gt;does&lt;/code&gt;). These interactions or behaviours are extracted to separate Role objects. Roles are then assigned to data objects at runtime, only when required within given Context.&lt;/p&gt;

&lt;a name='more'&gt;&lt;/a&gt;

&lt;p&gt;Here is an example of DCI implementation in Ruby:&lt;/p&gt;

&lt;script src="https://gist.github.com/1194608.js"&gt; &lt;/script&gt;

&lt;p&gt;The created &lt;code&gt;person&lt;/code&gt; object by default only describes a data model - what it is. By default this object is not capable of doing much more
than reporting it's state.&lt;/p&gt;

&lt;p&gt;In a &lt;code&gt;FootballGameContext&lt;/code&gt;, the &lt;code&gt;person&lt;/code&gt; object gains a new football player role and is able to &lt;code&gt;run&lt;/code&gt; and &lt;code&gt;kick_ball&lt;/code&gt;.
In a &lt;code&gt;Shop&lt;/code&gt; context, the same &lt;code&gt;person&lt;/code&gt; object doesn't act as a football player anymore, but becomes a buyer.&lt;/p&gt;

&lt;p&gt;To understand DCI well, I suggest starting with this &lt;a href="http://en.wikipedia.org/wiki/Data,_Context,_and_Interaction"  target="_blank"&gt;wikipedia article&lt;/a&gt; and if you have enough time, it's worth reading &lt;a href="http://www.artima.com/articles/dci_vision.html"  target="_blank"&gt;the artima article on DCI&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I find DCI a very promising paradigm to address some of the issues with OOP, but I'm definitely not an advocate yet. I haven't seen any big enough application written using this paradigm. I have no proof that DCI leads to cleaner and more readable code, than the traditional OOP with i.e. mixins or Design Patterns. Although it's worth investigating, it's still an immature paradigm and it's lacking good patterns to follow. One of the main problems, mentioned in one of &lt;a href="http://andrzejonsoftware.blogspot.com/2011/08/dci-patterns-how-to-write-dci-contexts.html" target="_blank"&gt;Andrzej Krzywda's articles&lt;/a&gt; on DCI in Ruby, is the ambiguity of when and how to define a Context.&lt;/p&gt;

&lt;h2 id="dci_and_inheritance"&gt;DCI And Inheritance&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)"&gt;Inheritance&lt;/a&gt; is one of the base OOP techniques. It is often a perfect way to describe "is-a" relationships in a data model. There is a very good answer on StackOverflow, describing &lt;a href="http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance/53354#53354"  target="_blank"&gt;when to prefer inheritance&lt;/a&gt; over composition.
Inheritance can lead to unmaintainable code when abused, hence it is considered evil in some circles of programmers.&lt;/p&gt;

&lt;p&gt;There are many other techniques which can be used as alternatives to inheritance, like &lt;a href="http://en.wikipedia.org/wiki/Mixin"  target="_blank"&gt;Mixins&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Strategy_pattern"  target="_blank"&gt;Strategy Pattern&lt;/a&gt; or &lt;a href="http://en.wikipedia.org/wiki/Adapter_pattern"  target="_blank"&gt;Adapter Pattern&lt;/a&gt;.
I don't want to argue whether inheritance is evil or if other techniques are better. In my experience, for each of these techniques there are scenarios where they fit best, including inheritance.&lt;/p&gt;

&lt;p&gt;One of the issues that DCI addresses, is overusing inheritance to give objects additional behaviours. In the code example I gave earlier,
&lt;code&gt;FootballPlayer&lt;/code&gt; would become a subclass of a &lt;code&gt;Person&lt;/code&gt;.&lt;/p&gt;

&lt;script src="https://gist.github.com/1194654.js"&gt; &lt;/script&gt;

&lt;p&gt;This works just fine if a &lt;code&gt;person&lt;/code&gt; object is used only as a &lt;code&gt;FootballPlayer&lt;/code&gt;. Problems start when the same &lt;code&gt;person&lt;/code&gt; object is also expected
to buy in a shop. We need to give it &lt;code&gt;Buyer&lt;/code&gt; behaviour. Using only inheritance we would either have to
create some kind of &lt;code&gt;FootballPlayingBuyer&lt;/code&gt; using multiple inheritance or make &lt;code&gt;Buyer&lt;/code&gt; a subclass of FootballPlayer.
You can imagine what happens is if we add more responsibilities.&lt;/p&gt;

&lt;p&gt;Although these problems can be solved in many ways, i.e. using mixins, the DCI paradigm guarantees these problems will never occur. In DCI, every behaviour (Role) has to be encapsulated in a separate object and is only added when context requires. DCI is indeed against inheritance - inheritance of behaviours.&lt;/p&gt;

&lt;h2 id="dci_doesnt_affect_data_model"&gt;DCI Doesn't Affect Data Model&lt;/h2&gt;

&lt;p&gt;DCI separates data model from behaviour, but it doesn't set any rules on how the data model should be designed.
The following example shows how DCI and inheritance can coexist together.&lt;/p&gt;

&lt;script src="https://gist.github.com/1194652.js"&gt; &lt;/script&gt;

&lt;p&gt;&lt;code&gt;Ship&lt;/code&gt; and &lt;code&gt;Airplane&lt;/code&gt; are two types of a &lt;code&gt;Vehicle&lt;/code&gt; and can always be used where &lt;code&gt;Vehicle&lt;/code&gt; object can be used. Subclassing seems to be a natural choice here. Both objects are still just dumb state containers and they get required behaviours only when needed.&lt;/p&gt;

&lt;p&gt;My bottom line is that DCI not only depends on well designed data model, but it actually helps to design it, by separating the behaviour and allowing programmer to focus on a data model alone.&lt;br/&gt; Inheritance can be sometimes the best fit to describe some parts of a data model and can help application to reflect user's mental model, which is one of DCI goals.&lt;/p&gt;
&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1114076417496162061-3847400104010425233?l=www.bartoszblimke.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.bartoszblimke.com/feeds/3847400104010425233/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.bartoszblimke.com/2011/09/dci-is-not-against-inheritance.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1114076417496162061/posts/default/3847400104010425233?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1114076417496162061/posts/default/3847400104010425233?v=2" /><link rel="alternate" type="text/html" href="http://www.bartoszblimke.com/2011/09/dci-is-not-against-inheritance.html" title="DCI Is Not Against Inheritance" /><author><name>Bartosz Blimke</name><uri>http://www.blogger.com/profile/08810234693165301502</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="23" height="32" src="http://bp1.blogger.com/_sAj7ObFDriQ/R6ZLoYaQFuI/AAAAAAAAAAM/LDXAyvyUaSU/S220/masb2.jpg" /></author><thr:total>0</thr:total></entry></feed>

