How do I construct a global counter?

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

How do I construct a global counter?

Postby the.drizzle » Thu Oct 18, 2007 10:19 pm

OK, here's a question;

Let's say I have a program (using the behavior API) that works like:

Code: Select all
Behavior b1 = new State1();
Behavior b2 = new State2();
Behavior b3 = new State3();

Behavior [] choices = {b1,b2,b3};
Arbitrator arb = new Arbitrator(choices);
arb.start;


where the 3 states basically work as such; behavior 1 and 2 depend on a boolean value to determine which behavior will occur, while behavior 3 will kick in once a counter--let's say the number of times behavior 2 has occurred--gets to a preset value.

The question is, then, how would I make this happen? Clearly, the counter value for behavior 3 would be passed to takeControl() in behavior 3, but the question I have is *how* do I actually make a global variable (lets call it counter) that can be iterated in the class file for behavior 2, and looked at in the class file for behavior 3?

Sorry if this is a really dumb question, my knowledge of JAVA programming is still quite limited...

Cheers!
the.drizzle
New User
 
Posts: 9
Joined: Wed May 02, 2007 10:59 pm

Re: How do I construct a global counter?

Postby roger » Fri Oct 19, 2007 6:18 am

the.drizzle wrote:OK, here's a question;

Let's say I have a program (using the behavior API) that works like:

Code: Select all
Behavior b1 = new State1();
Behavior b2 = new State2();
Behavior b3 = new State3();


Behavior 3 will kick in once a counter--let's say the number of times behavior 2 has occurred--gets to a preset value.

The question is, then, how would I make this happen?

Cheers!

As I see it, b3 needs to know about state changes in b2. So the problem is to establish an information flow path.
There at least 3 ways to do it
1. b2, when its counter changes value, sends the information to b3. To do this, b2 needs a reference to b3 and b3 needs to implement a public method like setCounter().
2 The reverse flow: b3 has a reference to b2. b2 maintains the counter and implements public method like getCounter(). Whenever b3 wants to know the value of the counter, it makes the query.
3 A third object acts like a bulletin board and implements both the getCounter() and setCounter() methods. b2 and b3 both have a reference to this third object. b2 updates the counter in the bulletin board when its state changes, and b3 queries when it wants to know. This is probably the scheme you had in mind. The role of bulletin board could be played by the object that contains the code in your example.

Instead of setter and getter methods, you could use public fields.
But any information path between objects requires that one has a reference to the object at the other end.

I hope this helps.
Roger
roger
Moderator
 
Posts: 350
Joined: Fri Jun 01, 2007 4:31 am
Location: Berkeley, CA


Return to NXJ Software

Who is online

Users browsing this forum: Baidu [Spider] and 1 guest

more stuff