adam bien's blog

Fluent Interface Pattern with EJB 3 and JPA - a working sample 📎

The Fluent Interface Pattern is nothing else, but the revival of object oriented development. However if you mention "object orientation", you will get an answer "It's nothing new" and still see Getter/Setter programming style and anemic objects (actually structures) :-). However with Java EE 5 it is possible again to build object oriented systems internally and expose them as services to the outside world. Building good, fluent APIs requires you to understand the domain. So it is really hard to provide reasonable samples for "Fluent Interfaces".

The following example is a snippet of a standalone JUnit-Test in the RunAndBikeDB sample project in p4j5:

    @Test
    public void addNewTraining(){
            String userName = this.existingUser.getUserName();
            String password = this.existingUser.getPassword();
            this.entityTransaction.begin();
 
            training.login(userName,password).
            running().
            today().
            averagePulse(135).
            comment("ok").
            length(65).
            maxPulse(165).
            km(12).
            add();

            this.entityTransaction.commit();
        }
You should understand the highlighted snippet, without having deep knowledge in Java EE 5 or even Java. What you do not see, are several JPA entities and a EJB 3 working behind the scenes for you. I'm working now on JSF and Swing frontend - using the MVP (rather Passive View) Pattern - this integration will be explained in the next JavaSpektrum (February) magazine (and this blog) more deeply. I will explain this pattern at the free Java User Group / Netbeans at at 24.01.2007 in Munich as well. The whole, deployable project is available freely from p4j5 (svn).