3D-kreativ
3D-kreativ

Reputation: 9301

Some questions about OOP in Java

I have a GUI class with a menu of buttons and textfields. Depending on what choices that is made in the menu and the input, methods in the GUI class are calling methods in the Logic class to send the input and create new objects of Customer class and Account class and so on.

To be able to communicate between the GUI- and the Logic class, I first create an object of the Logic class and I do that inside the GUI class, since it's here I have my main method. It this the best way to do it? Do I need some kind of reference variable between GUI- and Logic class or just use the reference when the object was created in the beginning of the GUI class? I guess to be able to communicate with a class, it must be an object first!? Thanks!

Logic logic = new Logic(); 

logic.addCustomer(name, number);

Upvotes: 0

Views: 183

Answers (7)

toto2
toto2

Reputation: 5326

I would instantiate the Logic class outside the GUI, but pass it as an argument to the GUI constructor. It's nearly equivalent to what you are already doing, but I think it makes it clearer that the GUI uses a Logic object. Also, it's possible that Logic does some other things before/after the GUI starts/closes; it might not be the case now, but it could be true in the future if you extend your program.

Many other answers tell you to look at MVC, but that might be overkill for your project. It can decrease complexity for a large project, but increase it for a small one.

EDIT:

Logic login = new Logic();
...
MyGUI gui = new MyGUI(logic);
...

Upvotes: 1

talnicolas
talnicolas

Reputation: 14053

I would suggest you do some researches on the MVC architecture. Your GUI (view) shouldn't interact directly with your model (logic). Implement a controller that will get the "signals" from your view and will be in charge to create your "logic objects" and work with them.

Upvotes: 1

andrey
andrey

Reputation: 842

Generally you can. If your application is very simple.

But this approach is not scalable. As your application gets more complex it became much harder for development and support. Try to consider Model–view–controller pattern to define a best way for your design. (according to your nick name I'll take a risk to propose an alternative link)

Upvotes: 0

Scorpion
Scorpion

Reputation: 3976

Ideally you shouldn't directly create the logic class. You should break down the functionality into a number of small classes, each of which satisfy a responsibility.

A simplistic way would be for the GUI class to create listeners which listen to the user events. In response the to the use event they fire events that your logic registers itself for. Then when the event is received the logic class can perform the functionality. You should read about observer pattern, event driven design...

You can read about event driven programming here http://en.wikipedia.org/wiki/Event-driven_programming .

Upvotes: 2

Gopal Nair
Gopal Nair

Reputation: 840

By default, Java uses Reference variables. Hence, if you instantiate your object in GUI class, make sure you send the object via method calls to your processing class.

Alternatively, you can look into singleton classes, which will return only one instance of the class. Inside that class, instantiate all the objects that you will need globally, and re-use that instance throughout your program.

Upvotes: 0

aseychell
aseychell

Reputation: 1804

You should look up the Singleton design pattern for such trivial scenarios.

Upvotes: 0

soupdiver
soupdiver

Reputation: 3673

You can create on object of type Logic in your main and store a reference of the object in your Window object - so you can access your Logic object as long as the window exists.

Upvotes: 0

Related Questions