java - Handle System as a dependency -


is there way java.lang.system used in dependency injection?

currently build classes

public class myclass {     private final dependency idependency;      public myclass(dependency pdependency)     {         this.idependency = pdependeny;     }      public static myclass createinstance()     {         final dependency tdependency = dependency.createinstance();         final myclass tinstance = new myclass(tdependency);         return tinstance;     }      // ... class methods ... } 

and create using:

myclass tmyclass = myclass.createinstance(); 

and test use:

@test public void testmethod() {     // setup     dependency ttestdependency = new testdependency();     myclass tmyclass = new myclass(ttestdependency);     // test     tmyclass.method(); } 

now have problem have java.lang.system dependency want write console logger. have no problem setting system datatyp dependency eclipse says no when want set system parameter. error "system cannot resolved variable".

public class consolelogger {     private final system isystem; // works      public myclass(system psystem) // works     {         this.isystem = psystem;     }      public static consolelogger createinstance()     {         final consolelogger tinstance = new consolelogger(system); // here error: system cannot resolved variable         return tinstance;     }      public void loginfo(string pmessage)     {         final string tprintmsg = this.isystem.currenttimemillis() + " info - " + pmessage;         this.isystem.out.println(tprintmsg);     } } 

i set system.err , system.out dependencies datatype printstream system.currenttimemillies() still not covered.

somebody idea how handle system dependency in scenario?

the system class contains several useful class fields , methods. it cannot instantiated.

i think have make interface , use delegate implementation. then, won't quite same because can't have static methods.

interface mysystem {     printstream getout();     inputstream getin();     .... }  class mysystemimpl {     printstream getout() {         return system.out;     }     ... } 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -