java - Do we need an interface/contract if we cannot generalize method parameters -
i want create interface having 2 methods, uploadfile
, downloadfile
. while need implementors implement these 2 methods, not sure , want care arguements these methods need take. mean, different implementors may ask different parameters. in case, should still go ahead creating interface making above methods var-arg methods, below
boolean uploadfile(object ... parameters) outputstream downloadfile(object ... parameters)
or there better approach this? right create interface if cannot generalize method parameters? sure method names , return types.
this might use case generics. consider following arrangement of classes - here define abstract "parameter" type , reference in interface. concrete classes work particular parameter set.
abstract class handlerparams { } interface filehandler<t extends handlerparams> { boolean uploadfile(t parameters); outputstream downloadfile(t parameters); }
example implementations:
class urlparams extends handlerparams { // whatever... } class urlfilehandler implements filehandler<urlparams> { @override public boolean uploadfile(urlparams parameters) { // ... } @override public outputstream downloadfile(urlparams parameters) { // ... } }
i must admit, i'm struggling imagine scenarios arrangement helpful. suppose have works file handlers, feels little artificial:
class somethingthatusesfilehandlers { public <t extends handlerparams> void dosomething(filehandler<t> handler, t params) { handler.downloadfile(params); } }
Comments
Post a Comment