creating abstract classes and guis
Hey everyone.
I'm pretty new to the OO type of development, so please excuse how bad my question is.
So here's the deal. I want to write a password manager program (yes, i know they already exist). There will be 3 JDialogs (add, remove, and view) that will all have the same look and style. the only difference will be the button that is displayed at the bottom (i.e add/remove/etc). Since they all deal with password objects, i figured i could write an abstract class that contained the username, password, and programName variables as well as an abstract doAction() method that will be overridden with the particular window action.
Does that make sense so far?
Now onto the real question. is it possible to create the generic GUI that the derived classes will inherit and be able to add the last action button for that window?
Thanks for any input
Jason
[901 byte] By [
jmgreen7a] at [2007-10-2 21:36:16]

when you say the button will be different, what you really mean is that the label on the button, and the action performed when clicked will be different. if the only real difference between the 3 dialogs is this, and maybe the title bar, why not just use one dialog, and have the actionPerformed method delegate its work to a worker object? you define an interface ( eg. DialogWorker) that has one method, doAction(), and your dialog carries a reference to an instance of this interface. define three classes that all implement this interface, called something like AddWorker, RemoveWorker and ViewWorker. when you launch the dialog, you could inject it with an instance of the correct class, and the right action would be performed when the button is hit
did I explain that ok? I tend to ramble, especially on a Friday!
sorry, that's a terrible explaination!
public interface Worker {
public void doAction();
}
public class ViewWorker implements Worker {
public void doAction() {
}
}
public class AddWorker implements Worker {
public void doAction() {
}
}
public class RemoveWorker implements Worker {
public void doAction() {
}
}
public class MyDialog extends JDialog implements ActionListener {
private Worker worker;
public MyDialog(Worker worker) {
this.worker = worker;
}
public void actionPerformed(ActionEvent arg0) {
worker.doAction();
}
}
public class Launcher {
public void launchViewDialog() {
MyDialog dialog = new MyDialog(new ViewWorker());
}
// other methods here
}
that's a very simplified design, but it should give you a good indicator
> That makes plenty of sense. How would i go about
> assigning the correct text on the JButton and the
> correct title bar?
that could all be set up when launching the dialog. probably the best way is to pass that text into the constructor, too.
Would the worker interface be hte
> one to hold my password objects? (name, password,
> etc)?
you could do that, but I wouldn't. best not to have too much functionality crammed into one object, especially unrelated functionality. without knowing more detail, it's hard to say how to design that in, but your worker interface could have a setCredentials() method that you pass those details in, I suppose, to carry them to wherever they go next