java - Design decision: extending an interface vs. new interface -


hallo,

i have little design decision today: there existing interface, called 'targetsystem' have 1 method 'getname()'. there no other common information these target systems. have new kind of target systems need authentication.

i have know whether target system needs authentication or not (the frontend have show password dialog those). if needs authentication, have set username , password.

my design decision: should extend existing interface methods 'needsauthentication' , 'setusernameandpassword' or creating new interface extending old 1 method 'setusernameandpassword', getting authentication need instanceof.

important: there no need downwards compatible or other reason not touch old interface! discussing co-worker, way nice one: creating interfaces names 'objectwithfeaturex', 'objectwithfeaturey' or creating methods 'hasfeaturex', 'hasfeaturey'.

i don't agree with peter. sometimes, instanceof can play central role in design.

personnaly, love following pattern (flameproof suit: "on"):

interface authentifiable {     void authentify(...) }  interface stateful {     void savestate(...)     void loadstate(...) }  interface myotheraspect {    ... } 

and then, in code:

void somecode() {   (server s : servers)   {     if (s instanceof authentifiable)        ((authentifiable) s).authentify(...)     if (s instanceof stateful)        ((stateful) s).load(...)     ...   }    (gridsystem gs : grids)   {     if (gs instanceof authentifiable)        ((authentifiable) gs).authentify(...)     if (gs instanceof stateful)        ((stateful) gs).load(...)     ...   } } 

this enables have orthogonal "aspects" working on object. can have object implementing feature & b, others b & c , others & c. ...or combination of features. if have many such features, comes in particularly handy. making 1 big interface of them implementing objects handle these features empty stubs might ugly.

plus, here, can check whether particular object has particular feature, can use directly, example split list of objects 2 bunches, 1 feature x , other without, in order process them differently.

the parts of system operating on these features need check if object has properties a, b or c checking instanceof. scalable, backward compatible , easy.

that said, specific way handling things , not suited general purpose stuff. suited if have lot of orthogonal features applying on several distinct objects.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -