Over a million developers have joined DZone.

Making Rich Domain Models in Ruby is Better than in Java

DZone's Guide to

Making Rich Domain Models in Ruby is Better than in Java

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

Being a Java developer starting to do some programing in Ruby i found many different nice things in the language and the way things are done.

One of the nicest things i found is the notion of module mixing, and although you can artificially do the same with Java and Spring (google for @DeclareParents) , it’s not as neat and clean as Ruby does it.

Module mixins basically allow us to add behaviour to our objects without inheritance. Is like if in Java we could use an Interface that has a default behaviour associated to it, so we don’t need to actually implement the methods of the interface.

The objects that has the module mixed in, can receive method calls on the module like if the methods belong to the object itself. This allows to a nice approach to domain rich programming, while at the same time maintaining a great separation of concerns and avoiding filling the classes and objects with lots of not very related functionality.

To explain the last paragraph better, let’s take a simple example. a Person class.

In our example a person can have emotions (happy, sad, etc), can do actions (play, sleep), have measure attributes and queries(height,weight). It can also be used in database operations (If we want like an ActiveRecord approach) (save, update, etc).

In a Model centric approach (Instead of using services, DAOs, etc) we would like to have Person objects that allow us to do thing like:

    if (person.sad?)
    if (person.higher_than?(person2))

We can consider each of these methods addressing different concerns, but all valid thing for a Person to know about. if we were to program this with Java, we would end up with a Person class with all these methods like:

    public class Person{
        public boolean isSsad(){...}
        public void playWith(Toy toy){...}
        public int store(){...}
        public boolean isHigherThan(Person person2) {...}

  We could argue that a better approach would be to have “behavioural” modules and plug them individually to our class. For example in Ruby we could have something like:

    class Person
       include Sentiments
       include Actions
       include Persistence
       include Measures

 We can have then each module dealing with its own concern, and making all this knowledge and capacities available to the Person objects as if they were defined on the Person class.

So for example our “Measures” module could have something like:

    module Measures
      attr_accesor :height  
      def higher_than?(entity)
       return self.height > entity.height

  Then we can do something like:

    a= Person.new
    a.height = 150
    b.higher_than? a

 Source: http://cscarioni.blogspot.com/2011/03/making-rich-domain-models-in-ruby-is.html

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}