0 ratings0% found this document useful (0 votes) 55 views5 pagesJava Fluent Api Design Example - Restaurant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
DZone, Log In Join
Feet
Design a Fluent API in Java Fearlessly.
Learn how to design an API that invokes a series of methods, making i fluent, wile using
Java
e
te a5) © commenti} Se save Tet 2 £7404 Ves
cn he Zone community nl ge the fl member experince
In this ate we wl discuss how to desig fluent AP in ava. The term lent nterfoce was coined by
‘Martin ower and Ere Evans Fluent APL neato build a APL in sich way so that t mets the following
+ The APL ser can understand the APL very easy
+The API can perform a serlesofactlons in ofder to fish task, In Java, we can dae witha series of|
method calls (cbaining of methods).
+ Each method's name shouldbe domain-specific terminology.
Suppose you want to design an APL ora domal, say Rta so there shouldbe some omion
terminology that exists the Retail omsin an for certain content (Tas) tl take series of
Now when you design an API you should design it sucha way that when APL users al Bling Servic for
invoice generation the users can faenty perform each tp in onder to complete said generation andthe
‘APL will ait users to perform those step upon invoking Bling Service
\When an API method snvoked by a user, the method wil perform its asad returns a Domain Objet,
‘which wil assistin what to co nest — until al tepsareeancuted, Unlike a standard API isthe APL
‘se'sjb to cll AP methods in sequential way to successfully performs task Sothe API usershave to
iow about the servic step very wll
Design A Fluent AP:
one)
ren
Ct
Poe)
QExample Suppose we want to design an/APLSFS RSE ant
‘As acustomer of this restaurant one should fellow these steps:
e -
8
a
@-@
Ina standard API design, we wil do the following
+ create "Restaurant interface,
+ Create an fimplementaton cass of Restaurant Compose the Mencar las into
+ Create getters and setters for restaurant properties ke name, addres. ee
+n MenuCard maintain a ist of tems Expose some methods ike showmenu),Ontermen( ee
+ Bach Mem has na
1nd cost properties and corresponding gtters/stters.
+ Whon the APL user calls thls API he/she wil calla sequence of methods(Enter Restaurant cll,
showMenu(). then Ordermenu) et) to perform the above steps shown nthe picture
Soitis nt fluent. lat of sequential statements nee to be performed to complete the task andthe APL
ser has to know the sequence
Now will show you how we vil design afuent APL
1. create an interface Resturant which has two methods
«Print ame ofthe restrantnotie the return type returns self because ater displaying name user
wants to see menu card
bs show( method returns menucant‘Thing to notice: Restarent Implementation Is suggestive as ithas two methods ones name and another
show (next operation user wants to perform)
2, IMenu Implementation hes 4 important methods shownnenu(}order(ea()y(-ll methods return
Menutlandor implementation sowe can perform one ofthese action. Again this is Suggestive.
ode Implementation
Java
fable tae arc (
te tetramer mes
2 ple etn
b
"soto concen contrac
orale earn ny)
le ten ele ny
)
pag oma aap ctr,
sie tae
,
Implementation
"ech: conceal
2 Ser on ee ees cotrat- en
bc cas tron leet Rett
,
Inner te tel 5 + nes
pte meme st
a
2
1 pcg oneal
Ste frattnserayot
2 te fmt te
2 art cance tap etree
‘unre enn map etree en
2 i a ramet em
© pic tnetometnse)|
* aiic ta nt
| Ct
« y :
Ne
as
a
Freep“ ra
af
3 sp itn Cs
st d :
te me at
] Of
>
cy
Sam tl « we mi
: sae
SOU& t
: »
a eae stiri;
my
Fe en reine de
> sme
E Sent pare pe
q| =r
oo
Spates et 4
= ¢
: spenomrinincetiog It
>
; *
SL pic sew pa 4
: t
=
Fyne en 6 a
oy °
Se ere
TS pe he wea ine
i Hela
o
fa
Tot pe wld sow
Bh fre as mua)
m ot
hey
my
Test Fluent API
fate cine riowtn f
peli mini we
n
o
‘Output:
Look How we perform the steps Nuenty by calling series of methodses Aesaan()name(ARSALANC) shou) -order(0).order(1).eat().pey()s
Tiss test example. To make it mare plished we need to work on the fllowing:
+ The Order method should take Menltem insted of postion, and italso needs to han Exception
scenarios if Menutem not found.
+ Order should beable ta be cancelled
cash)
+ Pay must have payment mode, (ered ar debit cas
+ Optional ips shouldbe incorporated.
+ While eating the user shoud be abet order more items, so make an Order functional during that
+ Twecaleulation should be added.
Like This Article? Read More From DZone
ae Bot APY Design With Java 8 Optional
re ee
ea
© me 35) Comments) Seve Tet = 47404 Vi
Integration Partner Resources
“Te Gene Guide to AP integrations: Explore Biakecsonaly syne contact data Between CRM,
P| intgratons Blow te Src eo) Norong, and Oaiabase endport