Solved

inform calling object

Posted on 2002-04-04
4
196 Views
Last Modified: 2010-03-31
Very rough example set up...

class A {
.
.
  B b = new B(123);
  // Add a listener here? so that when b.myID alters something happens.
.
.
}

public class B{
  int myID = 0;

  public B(setB){
    myID = setB;
  }

  public void setB(){
    myID = 312;
  }
}

How do I code so that when the value of myID changes in the instance of B then a method is called in the generating object of class A?

I'm guessing I'll need to add a listener of some kind to class A and a method to handle this in B???

Cheers,

Mal
0
Comment
Question by:malcx
  • 3
4 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
Comment Utility
There are lots of possibilities.

The straightforward one simply takes advantage of the use of the settor method. Since, presumably myID can only be changed by a call to setB, that method could call A to notify it of the change. B would need a reference to A to accomplish that of course. Something like:

class A {
.
.
 B b = new B(this,123);

public void bChange(int m){
   //do whatever is necessary here
}

.
.
}

public class B{
 int myID = 0;
 A ana;

 public B(A ta,setB){
   myID = setB;
   ana=ta;
 }

 public void setB(){
   myID = 312;
   ana.bChange(myID);
 }
}

Alternatively you could use a more generic mechanism like the Observer/Observable facilities. These would require that B be derived from Observable, and that A implements the Observer interface. B would then, in the setB method, make a call to notifyObservers which would call the update method in any registered observers. Something like:

class A implements Observer {
.
.
 B b = new B(this,123);
 b.addObserver(this);

public void update(B ab,Object a){
   //do whatever is necessary here
}

.
.
}

public class B extends Observable {
 int myID = 0;
 A ana;

 public B(A ta,setB){
   myID = setB;
   ana=ta;
 }

 public void setB(){
   myID = 312;
   notifyObservers();
 }
}

In this form it would require that the myID variable be inspectable from an A object. Alternatively you could get the value to it in an object wrapper by using the alternate form of notifyObservers, through which you can pass an object.

You could undoubtedly also cast this in the form of a Listener interface. On the other hand, Listeners as such were really designed for communicating system events. For the task at hand, it would probably come down to something very similar to an Observer/Observable pair.

0
 
LVL 3

Author Comment

by:malcx
Comment Utility
Great thanks!
Last time I programmed in Java I basically used it procedurally. I'm just undertaking another large project (after about 18 months spent with other languages...) and knew this'd be fairly simple.

Thanks again,

Mal
0
 
LVL 3

Author Comment

by:malcx
Comment Utility
Maybe I was a little hasty...
The code I'm currently working with is creating an instance of B from main() and as such won't let me use the non-static "this"...
???
Mal
0
 
LVL 3

Author Comment

by:malcx
Comment Utility
OK have fixed now,cheers
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now