Reputation: 1
I am relatively new to Java and am having trouble coming up with a truly object oriented design for a program I am working on. Here is a background to the program. The program is a gui based computer telephony application that monitors a phone and acts as a callerID (among other things). My current design is as follows. I have created the following classes.
MainWindow-this is the main window of the gui
Phone-this models the actual device
PopUpWindow-this models the pop up window that pops up on an event in the Phone class
PhoneMessenger-this sends a message to another device when an event happens in the phone class
The part I am having trouble with is that when an event happens in the Phone class I want it to update a value in the MainWindow class. I have the program working by constructing a static class variable MainWindow window in the MainWindow class and using static methods to change the values of the MainWindow class upon events in the Phone class. It works fine but I don't think this follows object oriented design principles as the Phone class now has access to all of the internal values of the MainWindow class. If someone could help me out with the design that would be amazing.
Upvotes: 0
Views: 565
Reputation: 9765
Have a look at my question. I was facing a similiar problem. However the users of SO are very smart. Have a look at this --> Print transaction status on TextArea
Upvotes: 0
Reputation: 7799
That's probably because you have all the classes in a single package and you are using no access modifiers and thus making all members package-protected
(perhaps package-public
is a better name). Use access modifier private
for member variables and/or separate classes in different packages more logically.
If I understand your design correctly, shouldn't you be using class PhoneMessenger
to deliver events? In this case, don't think in terms of "update a value in the MainWindow". Think in terms "an event X happened in the phone and I'm going to notify interested parties (MainWindow
)". When MainWindow
receives this event, it is its responsibility to decide what to do (change the value of the variable in this case), not class Phone
.
Upvotes: 1
Reputation: 2588
It sounds like you want to use a custom EventListener
object.
From the Phone
class, let's say you want there to be a ringing event that displays in the main window. Create an interface like this:
public interface RingListener {
public void ringing();
}
In the Phone class, create a method call addRingListener(RingListener listener)
and store listeners added in a List
, such as an ArrayList
.
In the MainWindow
class, store an instance of your Phone
object and add a RingListener
to it, implementing the abstract ringing method. Then type in your code for what happens.
phone.addRingListener(new RingListener() {
@Override
public void ringing() {
//Do stuff
}
}
In the Phone
class, whenever the event happens, iterate through all of the RingListeners
in your List and call the ringing method.
//Ringing happens!
for (RingListener listener : ringListeners) {
listener.ringing();
}
This is just a quick overview of how the EventListener
ideology works. I'd suggest taking a look at ActionListener
and see how it works, as well as searching for some tutorials on how to create your own custom listeners.
Upvotes: 1
Reputation: 10497
First, you need to aware about OPEN CLOSE PRINCIPLE. This is for oo design approach. and try to use interface which limits the scope of any class with defined methods. To access any object's value, always go for a POJO class. Here are the links which you needed how to design efficiently.
1.Open Close Principle
2. Interface Segregation Principle
3.other design principles
Upvotes: 0