Reputation: 121
Object A instantiates Object B.
After the user interacts with Object B, I want to return back to Object A because Object A needs to use the user-determined results in Object B. User answers questions in Object B.
Is there anyway to do that without instantiating a new Object A and passing the results in a constructor? Or is that the best method? I'm curious because wouldn't instantiating two Objects of the same type be wasteful?
I don't want to use inner classes because in addition to keeping Object A short, I want to have other Objects be able to use the Assignment class.I am using GUI for user interface. //Object A== Main Page
//Homework 1
if(e.getSource()==task[0]){
try {
AssignmentA hw1= new AssignmentA("questions.txt", 0); //creates Assignment to do
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
//Homework 2 Button
else if(e.getSource()==task[1]){
try {
AssignmentA hw2= new AssignmentA("questions.txt",1);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
//Object B==Assignment A
-creates questions and answers
-user picks answers and Assignment A stores the grade for the assignment.
Here's the scenario: Object A is the Main Page that allows you to access different assignments through buttons and contains the overall grade after doing multiple assignments. Object B is an Assignment Class. When one button is clicked, the Object B is instantiated and the user has to answer questions. Once the user finishes the assignment, I want to be able to return to the Object A/Main Page with a new overall grade without creating a new Object A.
I don't want to use an inner class because I have other pages that want to use the Assignment Class as well.
Upvotes: 3
Views: 844
Reputation: 285403
Some ideas:
Here's the scenario: Object A is the Main Page that allows you to access different assignments through buttons and contains the overall grade after doing multiple assignments. Object B is an Assignment Class. When one button is clicked, the Object B is instantiated and the user has to answer questions. Once the user finishes the assignment, I want to be able to return to the Object A/Main Page with a new overall grade without creating a new Object A.
ArrayList<B>
. Whenever a new B object is created and displayed, add it to the above list, which will allow A to iterate through the list whenever needed allowing it to query each B as to their states.BState
that can have possible values of INITIALIZED
, STARTED
, COMPLETED
, and give B a BState variable with getters and setters. This way A can iterate through the list and see what has been completed or not.BState
variable a "bound property" by giving B a SwingPropertyStateSupport field, and an addPropertyChangeListener(...)
method that adds these listeners to the support object. Then when B's BState changes, it can notify any and all listeners.You ask:
Is there anyway to do that without instantiating a new Object A and passing the results in a constructor? Or is that the best method? I'm curious because wouldn't instantiating two Objects of the same type be wasteful?
Upvotes: 1
Reputation: 53525
When object A instantiates object B it can pass itself as one of the parameters to the constructor.
When Object B is done it task - it can call a method on Object A telling it that it can continue.
class A {
void objectBisDone() {
//....
}
void createObjB() {
B b = new B(this); // <-- pass itself to B's constructor
b.run();
}
}
class B {
A myA;
B(A a) {myA = a;} // save A as a member
void run() {
// ...
// and when B is done
myA.objectBisDone();
}
}
Upvotes: 0
Reputation: 425003
Use a callback, something like:
interface Doer {
void doSomething(SomeClass data);
}
class B {
private Doer doer;
public B(Doer doer) {
this.doer = doer;
}
void run() {
if (something)
doer.doSomething(something);
}
}
class A implements Doer {
void doSomething(SomeClass data) {
// whatever
}
void callB() {
B b = new B(this);
b.run();
}
}
Upvotes: 1
Reputation: 8793
In OOP, any information that one abstraction shall make public, should be through public getter methods. I mean: Provide in class B as many get* methods as necessary, so that object A can invoke them.
Upvotes: 0