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){}