Monday, December 17, 2007

Blood Pressure Over 100

Fluent Interface in Egg Framework 2.0

One of the most exciting features introduced to Egg Framework 2.0 is a Fluent Interface - API, you can use to create readable code web applications in Java - to my surprise looking very similar to HTML.

Fluent Interface is a concept introduced by Martin Fowler and Eric Evans. They named this term to create a style of programming interfaces. More can be read on a blog Martin Fowler.

Here is an example web page created using the Fluent Interface implemented in Egg Framework 2.0 :

   package     org    .    eggframework2    .    view    .    examples    .    xhtml    ;  

import static org . eggframework2 . view . elements . xhtml . ext . FluentInterfaceMethods . * ;

import org . eggframework2 . view . elements . IElement ;
import org . eggframework2 . view . elements . xhtml . tags . Html ;

public class FluentInterfaceUsageExample extends Html {

public FluentInterfaceUsageExample ( ) {
IElement body = //
body ( //
p ( "Hello World" ) , //
a ( "Egg Framework Page" , href ( "http://eggframework.org" ) ) , //
form ( action ( "/action.do" ) , method ( "post" ) , //
input ( type ( "text" ) , name ( "Login" ) ) , br ( ) , //
input ( type ( "password" ) , name ( "Password" ) ) , br ( ) , //
input ( type ( "submit" ) , value ( "Please log me in" ) ) //
) , //
table ( //
tr ( //
td ( "Cell 1" ) , td ( "Cell 2" ) , td ( "Cell 3" ) //
) , //
tr ( //
td ( "Cell 1" ) , td ( "Cell 2" ) , td ( "Cell 3" ) //
) //
) ) ;

add ( body ) ;
}

public static void main ( String [ ] args ) { system. out. println ( new FluentInterfaceUsageExample ( ) ) ; }}

As you can see Java code is very clear and similar to the resulting HTML code. All thanks to the use of static methods and static imports. By importing all the classes FluentInterfaceMethods methods you can use them on your page as if they were part of it. We do not need so precede the name of the method name of the class in which it is defined. This makes code is much more concise.

FluentInterfaceMethods class methods in addition to creating instances of the elements (tags) HTML has a method of forming attributes such as type , name, id . This allows you to create a one line item and set its attributes.

If you are frequently placing the source code (Pretty Print) then you must be prepared for the fact that most of the IDE will work out the code that page with one line (because all the content by creating a nested method calls). It certainly does not help to increase readability of the code, just the opposite - it significantly worse. To guard against this situation, use the comments ("//") in places where the IDE is to break the line. Thanks to them, even after the laying operation code, the code will be nicely presented.

Fluent Interface in Egg Framework is a way to increase the readability of the code is created. It is a kind of overlay, and uses the existing API. After several / several minutes of work you should get used to this "strange style of programming." Depends on you whether you used it or not - in many cases may prove to be better, but there may be situations where the creation of content pages "in the usual way" would be easier and faster.

0 comments:

Post a Comment