Scala design suggestion needed -
i design client talk rest api. have implemented bit call http methods on server. call layer, api layer. each operation server exposes encapsulated 1 method in layer. method takes input clientcontext
contains needed information make http method call on server.
i'm trying set interface layer, let's call clientlayer
. interface 1 users of client library should use consume services. when calling interface, user should create clientcontext
, set request parameters depending on operation willing invoke. traditional java approach, have state on clientlayer
object represents clientcontext
:
for example:
public class clientlayer { private static final clientcontext; ... }
i have constructors set clientcontext
. sample call below:
clientlayer client = clientlayer.getdefaultclient(); client.executemymethod(client.getclientcontext, new mymethodparameters(...))
coming scala, suggestions on how have same level of simplicity respect clientcontext
instantiation while avoiding having state on clientlayer
?
i use factory pattern here:
object restclient { class clientcontext class mymethodparameters trait client { def operation1(params: mymethodparameters) } class myclient(val context: clientcontext) extends client { def operation1(params: mymethodparameters) = { // here based on context } } object clientfactory { val defaultcontext: clientcontext = // set here; def build(context: clientcontext): client = { // builder logic here // object caching can used avoid instantiation of duplicate objects context match { case _ => new myclient(context) } } def getdefaultclient = build(defaultcontext) } def main(args: array[string]) { val client = clientfactory.getdefaultclient client.operation1(new mymethodparameters()) } }
Comments
Post a Comment