Justin Loh’s Tech Blog

July 3, 2007

Explanation of Method Chaining

Filed under: Object Relational Mapping — jusloh @ 4:38 pm

Method chaining is a programming style supported by
many Hibernate interfaces. This style is more popular in Smalltalk than
in Java and is considered by some people to be less readable and more
difficult to debug than the more accepted Java style. However, it’s convenient
in many cases, such as for the configuration snippets you’ve seen in the following code:

SessionFactory sessionFactory = new Configuration()
.configure("/persistence/auction.cfg.xml")
.setProperty(Environment.DEFAULT_SCHEMA, "CAVEATEMPTOR")
.addResource("auction/CreditCard.hbm.xml")
.buildSessionFactory();

If you do use this coding
style, it’s better to write each method invocation on a different line. Otherwise,
it may be difficult to step through the code in your debugger.

Another more complex example with a method, getEntitleReferenceService(),
that returns an object of type interface, EntitlementsReferenceService, which has
method getUser(String userID). A concrete class of RemoteEntitleReferenceService,
implements the EntitlementsReferenceService interface, thereby enable the use of
getUser(String userID).


boolean b = ( Services
.getInstance()
.getEntitleReferenceService()
.getUser(username)
!= null );

Step by Step Explanation:

Services.getInstance() returns an object of type: Services

On the Services object, I call the method: getEntitleReferenceService()

this method returns an object of type interface: EntitlementsReferenceService
The only class that implements that interface is: RemoteEntitleReferenceService.java

In RemoteEntitleReferenceService.java resides the method: getUser(String userID)

with method signature:

public User getUser(final String userId){}

Advertisement

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.