Solving Object-Oriented Java System Issue

  • Thread starter ramollari
  • Start date
  • Tags
    Programming
In summary: It might have some time overhead, but it is clean design and is very good practice particularly for big systems.In summary, using polymorphism in Java can be tricky. It's not a perfect solution, but there is a way to implement a globally callable method that allows lower-level objects to pass messages to the user. However, this method is not the easiest to implement.
  • #1
ramollari
437
1
Hi! I'm trying to build an object oriented system (in Java). There are several classes that provide the functionality of the system and GUI classes that use this functionality. The issue is how will the inner classes notify the GUI when events happen (like finishing reading from the disk, or a new message from the network), since they aren't aware of the existence of GUI classes and cannot call its methods. :confused:
 
Computer science news on Phys.org
  • #2
Java doesn't have polymorphism like C++, so it's tricky.

Can't you implement a globally callable method within the GUI part that allows the lower level objects to pass messages to the user? Kind of like an alert box in javascript, or a Windows message box.

It's not a perfect solution, I know, as it goes against the object oriented idea.
 
  • #3
Hi Ceptimus,

ceptimus said:
Java doesn't have polymorphism like C++, so it's tricky.

I don't see the need for polymorphism except that there could be a general GUI class to which inner classes send messages.
By the way polymorphism is supported in Java. A superclass reference can point to objects of any of its subclasses.

ceptimus said:
Can't you implement a globally callable method within the GUI part that allows the lower level objects to pass messages to the user? Kind of like an alert box in javascript, or a Windows message box.

It's not a perfect solution, I know, as it goes against the object oriented idea.
I would need several such methods to make them global/static like a message box, flashing sign, drawing an image, etc. In this case the inner classes must have knowledge of the top level GUI class. On the other hand these classes can have a pointer/reference to an object of the GUI class to call its methods, but I don't consider this as good practice.
 
  • #4
I usually maintain a class wherein i keep a lot of flags (static) ...
i update these flags as an when events occur ... all classes refer this "flag class" periodically and take actions accordingly. This is one way of message passing.

-- AI
 
  • #5
TenaliRaman said:
I usually maintain a class wherein i keep a lot of flags (static) ...
i update these flags as an when events occur ... all classes refer this "flag class" periodically and take actions accordingly. This is one way of message passing.

-- AI
No way. Keeping flags would require polling to check their values periodically. But this is costly because of more programming and more time overhead. I'd rather use a bidirectional association between the GUI class and the other classes. I just had thought that the inner classes could generate signals, maybe events or exceptions.
 
  • #6
I never said my method was fast, but yes definitely its the easiest.

If ur application is speed demanding then there is a way out using swing's invokelater
check this out,
http://forum.java.sun.com/thread.jspa?tstart=15&forumID=424&threadID=566282&trange=15

Hopefully this helps.

-- AI
 
Last edited by a moderator:
  • #7
TenaliRaman said:
I never said my method was fast, but yes definitely its the easiest.

I'm not sure it is the easiest. Maybe if you're just planning on writing a one-off and never touching it again. But if you actually expect to maintain the code then your method will likely end up being a lot of work.

You've basically just created a bunch of global variables and made every event method dependent on them - it sounds pretty fragile. Everything is interconnected, and race conditions could creep in really easily.



You could always do something where GUI classes register themselves with the low level classes, and then when the low-level class does something eventworthy it calls a method on all of the classes registered with it. The low-level events would have to track what's registered with it, but it wouldn't really need to have any understanding of the GUI classes.
 
  • #8
master_coda said:
You could always do something where GUI classes register themselves with the low level classes, and then when the low-level class does something eventworthy it calls a method on all of the classes registered with it. The low-level events would have to track what's registered with it, but it wouldn't really need to have any understanding of the GUI classes.
Yes, event handling is the optimal scheme. It might have some time overhead, but it is clean design and is very good practice particularly for big systems.
 
  • #9
ramollari said:
Yes, event handling is the optimal scheme. It might have some time overhead, but it is clean design and is very good practice particularly for big systems.

Well, it shouldn't have a huge time overhead. All you need (at least to start) is a standard handler interface and some simple event dispatching code.
 
  • #10
master_coda said:
I'm not sure it is the easiest. Maybe if you're just planning on writing a one-off and never touching it again.

LOL! You got it! :biggrin:

-- AI
 

What is an Object-Oriented Java System?

An Object-Oriented Java System is a programming approach that emphasizes the use of objects and their interactions to create software solutions. It allows for modular, reusable, and maintainable code by breaking down a problem into smaller entities called objects.

What are some common issues that can arise in Object-Oriented Java Systems?

Some common issues in Object-Oriented Java Systems include problems with class design, inheritance, polymorphism, and encapsulation. These can lead to errors in code functionality, performance issues, and difficulties with maintenance and scalability.

How can I approach solving an Object-Oriented Java System issue?

To solve an Object-Oriented Java System issue, it is important to first identify the specific problem and its root cause. Then, you can use debugging techniques such as print statements and stepping through code to pinpoint the issue. From there, you can make necessary changes to the code, test it, and continue to refine until the issue is resolved.

What are some best practices for avoiding Object-Oriented Java System issues?

To avoid issues in Object-Oriented Java Systems, it is important to follow best practices such as proper class design, clear and concise naming conventions, proper use of inheritance and polymorphism, and maintaining encapsulation. It is also important to regularly test and debug code and to continuously refactor and improve the codebase.

How can I improve my skills in solving Object-Oriented Java System issues?

To improve your skills in solving Object-Oriented Java System issues, it is important to practice regularly and actively seek out opportunities to work on projects that utilize this programming approach. Additionally, staying updated on industry developments and learning from experienced programmers can also help improve your skills.

Similar threads

  • Computing and Technology
Replies
6
Views
1K
  • Programming and Computer Science
Replies
3
Views
3K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
Replies
5
Views
223
  • Programming and Computer Science
Replies
5
Views
811
  • Programming and Computer Science
Replies
32
Views
5K
  • Programming and Computer Science
Replies
17
Views
2K
Back
Top