1

I know nothing about gui programming. I try to learn. I have simple question...

For example, I have small program. I have class that extends JFrame with main Window (MyWindowClass) and my "logic" class (MyClass) that do something (for example calculate something and I want to display it in window).

Should I implement this like:

class MyClass {
    // ...
    // ...
    MyWindowClass window;
};

Or like this:

class MyWindowClass extends JFrame {
   // ...
   MyClass c;
};

I don't know which class should be an owner and which class should be have main method...

Please help me...

I try to write in Java (Swing) but this is general question I guess.

migrated from stackoverflow.com Dec 16 '13 at 20:44

This question came from our site for professional and enthusiast programmers.

  • 2
    That would be a terrible design to put your UI components in your logic class IMHO. MVC pattern will help you to deal with this. – ZouZou Dec 16 '13 at 20:19
  • 2
    Try to search about MVC pattern. – Anirban Nag 'tintinmj' Dec 16 '13 at 20:20
  • Thanks for Your answers ZouZou and tintinmj. I will read and learn this design pattern. Maybe I will find there a something idea to my problem.... It looks better for me when GUI class is an owner "logic" class. But I don't know whoat to do with situation like this: I have for example network class. Something send to my class String. I received it. And I what to display it in my WindowClass (in JTextField). How to do that dynamically? I don't know when Someone send something to me. If MyClass is owner of GUI class this is very easy... – pawell55555 Dec 16 '13 at 20:35
3

Between your two options having the view know about your "logic" class would be the better method. This would allow you to create or modify your view without having to change your logic classes. If you put your view inside of your "logic" classes it dramatically limits your flexibility.

As people mentioned in the comments read up on the MVC pattern as that will demonstrate some ideal solutions.

1

Classical Model-View-Controller question.

MyClass is a model. It has the domain data and implements domain logic.

SWING components (JTextField, JTable) are a view. They know how to show data to the user and how to accept user input, but know nothing about your domain model.

MyWindowClass is a controller. It needs to mediate your model and view - e.g. set text field value to object property value or update object in response to button press.

Hence, MyWindowClass needs to be able to access both the model and the view. There is little reason for MyClass to know about the UI - e.g. you may use the same domain class in different UI (e.g. list of objects and edit form for a single object).

0

Ideally, wouldn't have either your logic class or your window class contain the other, but would instead use a "application" object to contain them both.

However, if for some reason that isn't practical, the choice of which object contains the other when both objects are from entirely different areas of concern should be determined by which object is dependent upon the other. If MyWindowClass will never run without a MyClass object, it may contain such as a member; if MyClass will never run without MyWindowClass, it may contain such instead.

(Although this should only be a concern when the overall application design doesn't allow for proper separation, which is an instance that occurs less and less frequently as languages and frameworks have evolved from the bad old days.)


As an aside, if you are forced into cross-concern composition, make sure that you keep the references as generic as practicable. You want to be able to substitute a MyModifiedClass for MyClass in MyWindowClass with a minimuim of fuss down the road.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.