Method Chaining in POJO/DTO/JPA/Hibernate Persistence Objects
Join the DZone community and get the full member experience.
Join For FreeIn recent years method chaining has become popular in the Java/JEE world. Frameworks that I am aware of, that have used Method Chaining effectively are, Hibernate/JPA,EasyMock/JMock and jQuery. jQuery has revolutionized the JavaScript world by using readable fluent interfaces. Method chaining has been around for years from the Small Talk era. In C++,it is very common practice to use method chaining in getter/setter classes. Let us see how we can apply method chaining to Java POJO classes.
Typical Java POJO Example
Let us consider following daily stock price example,DailyPriceData.java
public class DailyPriceData { private Date transDate; private Double open; private Double high; private Double low; private Double close; private Double volume; public DailyPriceData () { } public DailyPriceData (Date transDate, Double open, Double close) { this.transDate =new Date(transDate.getTime()); this.open = open; this.close = close; } public Double getClose() { return this.close; } public void setClose(Double close) { this.close = close; } ................./getters and setters }
The DailyPriceData class above is the typical way we generate POJO's or the way our IDE's will generate them. The Builder/Converter class that will construct the above object has to use setter methods or a constructor has to be provided. We certainly will not provide a bloated constructor with six arguments. The way we solve this issue generally is, we provide getters/setters and expect the Builder/Converter class to construct object using getter/setters. If we want object to be immutable, then we would break the object finer like Price(open,high,low,close)etc... and provide a constructor with finer nested objects. We will not discuss the later case here, and we will assume we are providing getters and setters in our typical Java POJO way.
Converter/Builder
In the Enterprise Java world, it is quite common to convert Persistence objects to DTOs or JAXB Beans etc.. Also it is quite common for us to write Builder classes which construct/build objects based on certain attributes or from other objects. See the example below how we construct the object below with normal setters,PriceBuilder.java
DailyPriceData priceData = new DailyPriceData (); priceData.setClose(priceBean.getClose()); priceData.setHigh(priceBean.getHigh()); priceData.setLow(priceBean.getLow()); priceData.setVolume(priceBean.getVolume());
POJO using Method Chaining
public class DailyPriceData { private Date transDate; private Double open; private Double high; private Double low; private Double close; private Double volume; public DailyPriceData () { } public Double getOpen() { return open; } public DailyPrice setOpen(Double open) { this.open = open; return this; } public Double getHigh() { return high; } public DailyPrice setHigh(Double high) { this.high = high; return this; } ....... }
As you can see the only difference between our classical Java POJO and the class above is ,it returns the object (DailyPrice in our case) that invokes the setter method. Now we will see how we construct the object in our builder class.
PriceBarData priceData = new PriceBarData().setClose(priceBean.getClose()) .setHigh(priceBean.getHigh()) .setLow(priceBean.getLow(). setVolume(priceBean.getVolume());
One of the major advantages of using this method is that if you try writing the above code in IDE's like Intellij or Netbeans you would see how fast you would write the code by tabbing :).. Also the technique above will not disturb your Hibernate generators,or Dozer frameworks.
Fluent Interfaces
Martin Fowler wrote an article based on Eric Evans presentation and named interfaces 'that are primarly designed to be readable and to flow' as fluent interfaces. Though, we use method chaining in the above method, it is not readable and flowable as we wish to describe as price().openedAt().closedAt().surgedAt()...etc... We will definitely break our Hibernate or JAXB frameworks,if we are using any fluent interfaces. As he describes in his article, writing a fluent interface requires good bit of thought.
Also you can find this article in my personal Blog Using Method Chaining in POJO/DTO/ Entities
Opinions expressed by DZone contributors are their own.
Trending
-
Merge GraphQL Schemas Using Apollo Server and Koa
-
13 Impressive Ways To Improve the Developer’s Experience by Using AI
-
Managing Data Residency, the Demo
-
Hyperion Essbase Technical Functionality
Comments