Home > Back-end >  Java programming problem for help, monitor code
Java programming problem for help, monitor code

Time:11-04

Data monitoring
*
* required to write a monitoring code, once a member of the foo change variable values (call your setX method),
* console output content before and after the member variable and changes, such as:
* foo in setA (1);//output: a: 0 - & gt; 1
* foo setB (2);//output: b: 1 - & gt; 2
* foo setC (3);//output: c: 0 - & gt; 3

@ Getter
@ Setter
Public class Foo {
Private int a;
Private int b;
Private int c;
Private int d;
Private int e;
Private int f;
}

CodePudding user response:

Methods a
Reference the observer pattern, in the foo class append a callback listener, every set method calls the listener to print
Method 2
Aop thought, using aop springboot annotations, intercept foo set methods, before the set method to print

CodePudding user response:

Solution a:
 package prop. Change. V1; 

Import the Java. Beans. PropertyChangeSupport;
Import the Java. Beans. PropertyChangeListener;

Public class Foo {

Private final PropertyChangeSupport _pcs=new PropertyChangeSupport (this);
Private int a;
Private int b;
Private int c;
Private int d;
Private int e;
Private int f;

Public void addPropertyChangeListener (PropertyChangeListener listener) {
_pcs. AddPropertyChangeListener (the listener);
}

Public void removePropertyChangeListener (PropertyChangeListener listener) {
_pcs. AddPropertyChangeListener (the listener);
}

Public void setA (int a) {
Int o=this. A;
Enclosing a=a;
_pcs. FirePropertyChange (" a ", o, a);
}
Public void setB (int b) {
Int o=this. B;
This. B=b;
_pcs. FirePropertyChange (" b ", o, b);
}
Public void setC (int) c {
Int o=this. C;
This. C=c;
_pcs. FirePropertyChange (" c ", o, c);
}
Public void setD (int) d {
Int o=this. D;
This. D=d;
_pcs. FirePropertyChange (" d ", o, d);
}
Public void setE (int) e {
Int o=this. E;
This. E=e;
_pcs. FirePropertyChange (" e ", o, e);
}
Public void setF (int) f {
Int o=this. F;
This. F=f;
_pcs. FirePropertyChange (" f ", o, f);
}

Public PropertyChangeSupport get_pcs () {
Return _pcs;
}

Public int getA () {
return a;
}

Public int getB () {
return b;
}

Public int the getC () {
return c;
}

Public int getD () {
Return d;
}

Public int getE () {
return e;
}

Public int getF () {
Return the f;
}

}

 package prop. Change. V1; 

Import the Java. Beans. The PropertyChangeEvent;
Import the Java. Beans. PropertyChangeListener;

Public class Useage {

Public static void main (String [] args) {
Foo Foo=new Foo ();
Foo setA (0);
Foo setB (1);
Foo setC (0);
Foo addPropertyChangeListener (new PropertyChangeListener () {

@ Override
Public void propertyChange (PropertyChangeEvent evt) {
System. Out.println (evt) getPropertyName () + ":" + evt. GetOldValue () + "- & gt;" + evt. GetNewValue ());

}});

Foo setA (1);
Foo setB (2);
Foo setC (3);

}

}

 a: 0 - & gt; 1 
B: 1 - & gt; 2
C: 0 - & gt; 3

CodePudding user response:

refer to the second floor ice rain reply:
solution a:
 package prop. Change. V1; 

Import the Java. Beans. PropertyChangeSupport;
Import the Java. Beans. PropertyChangeListener;

Public class Foo {

Private final PropertyChangeSupport _pcs=new PropertyChangeSupport (this);
Private int a;
Private int b;
Private int c;
Private int d;
Private int e;
Private int f;

Public void addPropertyChangeListener (PropertyChangeListener listener) {
_pcs. AddPropertyChangeListener (the listener);
}

Public void removePropertyChangeListener (PropertyChangeListener listener) {
_pcs. AddPropertyChangeListener (the listener);
}

Public void setA (int a) {
Int o=this. A;
Enclosing a=a;
_pcs. FirePropertyChange (" a ", o, a);
}
Public void setB (int b) {
Int o=this. B;
This. B=b;
_pcs. FirePropertyChange (" b ", o, b);
}
Public void setC (int) c {
Int o=this. C;
This. C=c;
_pcs. FirePropertyChange (" c ", o, c);
}
Public void setD (int) d {
Int o=this. D;
This. D=d;
_pcs. FirePropertyChange (" d ", o, d);
}
Public void setE (int) e {
Int o=this. E;
This. E=e;
_pcs. FirePropertyChange (" e ", o, e);
}
Public void setF (int) f {
Int o=this. F;
This. F=f;
_pcs. FirePropertyChange (" f ", o, f);
}

Public PropertyChangeSupport get_pcs () {
Return _pcs;
}

Public int getA () {
return a;
}

Public int getB () {
return b;
}

Public int the getC () {
return c;
}

Public int getD () {
Return d;
}

Public int getE () {
return e;
}

Public int getF () {
Return the f; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related