If you need to monitor certain states of the (monitor) class, you need to write a monitoring class and monitor the monitoring class.
But only locally, if remote monitoring is required, use proxy pattern.
Specific methods:
1. The class needs to provide state information and provide some get methods to make the call.
/** * @time July 11, 2014 * * Package proxy; /** * @author C.l.wang * */public class Gumballmachine {String location;//Location Information Stat
e soldoutstate;
State noquarterstate;
State hasquarterstate;
State soldstate;
State winnerstate;
state state = Soldoutstate;
int count = 0; /** * */Public Gumballmachine (String location, int numbergumballs) {//TODO auto-generated Co
Nstructor stub soldoutstate = new Soldoutstate (this);
Noquarterstate = new Noquarterstate (this);
Hasquarterstate = new Hasquarterstate (this);
Soldstate = new Soldstate (this);
Winnerstate = new Winnerstate (this);
this.location = location;
This.count = Numbergumballs;
if (Numbergumballs > 0) {state = Noquarterstate; }} public void InseRtquarter () {state.insertquarter ();
public void Ejectquarter () {state.ejectquater ();
public void Turncrank () {State.turncrank ();
State.dispense ();
public void SetState (state state) {this.state = state;
The public void Releaseball () {System.out.println ("A Gumball comes rolling out the slot ...");
if (count!= 0) count-;
public int GetCount () {return count;
public void refill (int count) {This.count = count;
state = Noquarterstate;
Public State getState () {return state;
Public String getLocation () {return location;
Public State getsoldoutstate () {return soldoutstate; Public State getnoquarterstate () {return noquarterstate;
Public State gethasquarterstate () {return hasquarterstate;
Public State getsoldstate () {return soldstate;
Public State getwinnerstate () {return winnerstate;
Public String toString () {stringbuffer result = new StringBuffer ();
Result.append ("\nmighty Gumball, Inc.");
Result.append ("\njava-enabled Standing gumball Model #2004 \ n");
Result.append ("Inventory:" + count + "Gumball");
if (count!= 1) {result.append ("s");
} result.append ("\nmachine is" + state + "\ n");
return result.tostring (); }
}
This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/
2. Monitor class, call get method, monitor output.
/**
* @time July 12, 2014 * *
package proxy;
/**
* @author C.l.wang
*
*
/public class Gumballmonitor {
gumballmachine machine;
/**
* */Public
Gumballmonitor (Gumballmachine machine) {
//TODO auto-generated constructor stub< C15/>this.machine = machine;
}
public void () {
System.out.println ("Gumball Machine:" + machine.getlocation ());
System.out.println ("Current inventory:" + machine.getcount () + "gumballs.");
System.out.println ("Current state:" + machine.getstate ());
}
3. Other code reference: http://blog.csdn.net/caroline_wendy/article/details/37698747
4. Test class, instantiate specific class, and use monitoring class to monitor.
/**
* @time July 11, 2014 * *
package proxy;
/**
* @author C.l.wang * */Public
class Gumballmachinetestdrive {
/**
* @param args
* * Public
static void Main (string[] args) {
//TODO auto-generated method stub
Gumballmachine Gumballmachine = new Gumballmachine ("Seattle");
Gumballmonitor gumballmonitor = new Gumballmonitor (gumballmachine);
Gumballmonitor.report ();
}
5. Output:
Gumball machine:seattle current
inventory:115 gumballs.
Current state:waiting for quater