java - Communication between several classes -
whenever make program tend divide different sections in different files, think looks more neat way. make problem more concrete have dummy code consisting of 4 classes,
public class dummy { public static void main(string[] args){ alpha = new alpha(); beta b = new beta(); gamma g = new gamma(); int x,y,z,j,k,l,o,p,q; x = a.getgammax(); y = b.getgammax(); z = g.getx(); a.setgammax(1); j = a.getgammax(); k = b.getgammax(); l = g.getx(); b.setgammax(2); o = a.getgammax(); p = b.getgammax(); q = g.getx(); } } class alpha{ gamma g = new gamma(); public int getgammax(){ return g.getx(); } public void setgammax(int x){ g.setx(x); } } class beta{ gamma g = new gamma(); public int getgammax(){ return g.getx(); } public void setgammax(int x){ g.setx(x); } } class gamma{ int x = 10; public int getx(){ return x; } public void setx(int y){ x = y; } }
the alpha , beta classes communicate gamma. purpose of integers print out values , compare them each other after setters have been called different classes. general problem have when split sections when 1 class change values in gamma, other classes have obsolete values, x,y,z variables still have old values of x in gamma when setter method has been called, unless discover before spending quite time finding it.
i tend circumvent passing around 1 reference of gamma class,
gamma g = new gamma(); alpha = new alpha(g); beta b = new beta(g);
like so, , spend several lines of code update everything. reference being created "main" class, "main" not class main method, class action is.
the question have is, there standard way of dealing kind of problem, multiple classes communicate 1 other class, changing values etc. , ending obsolete values?
my problems tend more subtle shown tend spend time on trying fix. sorry if explanation muddy, had hard time make problem clear, if incomprehensible, write in comments , try clear bit more clearer.
use mvc (model-view-control) pattern or 1 of variants, , have classes need hold same data share model. instance perhaps gamma model, have other classes share same gamma instance.
e.g.,
public class dummy { public static void main(string[] args) { gamma g = new gamma(); alpha = new alpha(g); beta b = new beta(g); // ......... } } class alpha { private gamma g; public alpha(gamma g) { this.g = g; } public int getgammax() { return g.getx(); } public void setgammax(int x) { g.setx(x); } } class beta { // gamma g = new gamma(); private gamma g; public beta(gamma g) { this.g = g; } public int getgammax() { return g.getx(); } public void setgammax(int x) { g.setx(x); } } class gamma { int x = 10; public int getx() { return x; } public void setx(int y) { x = y; } }
to have have classes notified of state changes on class, use, use observer pattern recommended johnb.
note 1 way gain observer support use javabean's propertychangesupport. example:
class gamma { public static final string x = "x"; private propertychangesupport support = new propertychangesupport(this); private int x = 10; public int getx() { return x; } public void setx(int x) { int oldvalue = x; int newvalue = this.x; this.x = x; support.firepropertychange(x, oldvalue, newvalue); } public void addpropertychangelistener(propertychangelistener listener) { support.addpropertychangelistener(listener); } public void removepropertychangelistener(propertychangelistener listener) { support.removepropertychangelistener(listener); } }
note: nicest , cleanest way inject , b classes shared gamma class via dependency injection such available 1 of spring's modules or guice framework.
Comments
Post a Comment