design - OOP accessing method from non-related class -


hello stackoverflowers,

i have 5 classes, foo, bar, thud, grunt, zot. thud , grunt instances field of bar. foo instance field of thud.

foo, thud , grunt prepare data view (mvvm pattern, view model). foo , zot datas or create them (the model)

among other things, foo produce zot listed in grunt (added bar, access foo thud). need foo list of zot in grunt. if possible avoid going work (will serialize list) in grunt or bar class because not model. process start foo (adding new classes or stuff of course possible)

public class bar {   thud thud;   grunt grunt;    bar(zot zotinstance)   {     new thud();     new grunt();     grunt.zotlist.add(zotinstance);   }  }  public class thud {   foo foo; }  public class grunt {   list<zot> zotlist;   public list<zot> getlist(); } public class foo {   public zot makezots() {};   public void bringmezots() // way zotlist when method called. } 

i'm not sure explain in simplest way. tell me if explanation of problem needed.

so need way share grunt object you're creating.

first need way inject foo class:

public class foo {   grunt grunt;    public foo(grunt g)   {     grunt = g;   }    public zot makezots()   {   }    public void bringmezots()   {     list<zot> herearesomezots = grunt.getlist();   } } 

which means need inject thud class also:

public class thud {   foo foo;    public thud(grunt g)   {     foo = new foo(g);   } } 

then lastly, can pass grunt instance thud instance:

public class bar {   thud thud;   grunt grunt;    bar(zot zotinstance)   {     grunt = new grunt();     grunt.zotlist.add(zotinstance);     thud = new thud(grunt);   } } 

you have pretty abstract class names it's difficult me tell each of these doing when see dependencies needing passed around much, signals issues overall design.

you should @ dependency injection simplify design.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -