Blogounage

Aller au contenu | Aller au menu | Aller à la recherche

Mot clé - design pattern

Fil des billets - Fil des commentaires

vendredi 21 décembre 2007

Quelles méthodes je peux mettre dans mes « objets du domaine » ?

Gavin King, le créateur du projet Hibernate, dont le hobby est maintenant devenu Seam, a récemment publié un billet intéressant sur ce sujet.

Souvent, la question se pose : quelles méthodes doit-on modéliser dans un objet métier ? Concrètement, où dois-je mettre mon code métier ? Par exemple, est-ce que la méthode de sauvegarde d'une facture doit se trouver dans l'objet Facture lui-même ? Si je veux récupérer le nombre de clients d'une facture, dois-je mettre la méthode dans la Facture, etc. ?

Gavin a formulé d'une façon très concise une règle à laquelle je souscris totalement :

the domain model (entity classes) are the most reusable classes in my codebase

[...]

In particular, I would never write code that calls out to external services, or accesses the database, or calls an EJB/Seam/Spring component in my entity class. I want my domain model to be completely self-contained!

So anytime you find yourself wishing that entities supported injection, or find yourself writing a JNDI lookup in a method of an entity, please consider that your domain model is no longer self-contained, and will be less reusable in different execution environments.

Rien de plus à ajouter.

dimanche 16 décembre 2007

Do Interfaces have semantics ?

When I saw the title of this entry in my feed reader, my first thought was who the f*** could be wondering if interfaces have semantics ?. Happily, that's not Gavin who thinks this.

The assertion is the following :

Interfaces in Java don't have any semantics attached. Only a concrete class can define the semantics of a method.

In my opinion, that's even what interfaces are designed for, defining semantics...

I totally agree with Gavin when he says semantics is defined in the Javadoc and the "name of the things". I always find myself taking a long time to carefully choose the naming of my objects, since I want those names to be the most meaningful possible (same for method parameters, method names, and so on. Naming meaninfully local variable is also important, but it goes beyond the scope of the current subject).

Using polymorphism let you see different implementations as the same thing, thanks to the defined contract. Maybe I'm only viewing interfaces throught the Liskov principle. But for me contract is a synonymous for semantics when it comes to programming, although I'd be glad to hear explanations of where it differs in your opinion.

Even more, I think that today, as everybody gets more and more used to using the dependency injection pattern, some concrete classes are not even known to the code they're used by. It's becoming common to use classes only through their interfaces only (services, for example). In those case, semantics resides in the interface only, and implementations obviously have to respect it very carefully.