简体   繁体   English

有关Java中OOP的一些问题

[英]Some questions about OOP in Java

I have a GUI class with a menu of buttons and textfields. 我有一个带有按钮和文本字段菜单的GUI类。 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. 根据菜单和输入中的选择,GUI类中的方法正在调用Logic类中的方法以发送输入并创建Customer类和Account类的新对象,依此类推。

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. 为了能够在GUI类和Logic类之间进行通信,我首先创建Logic类的对象,然后在GUI类内部进行操作,因为在这里有我的主要方法。 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? 我是否需要在GUI-和Logic类之间使用某种引用变量,或者仅在GUI类的开头创建对象时使用引用? 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);

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. 一种简单的方法是让GUI类创建侦听用户事件的侦听器。 In response the to the use event they fire events that your logic registers itself for. 作为对use事件的响应,它们会触发您的逻辑为其注册的事件。 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 . 您可以在http://en.wikipedia.org/wiki/Event-driven_programming上阅读有关事件驱动程序的信息。

I would suggest you do some researches on the MVC architecture . 我建议您对MVC架构进行一些研究。 Your GUI (view) shouldn't interact directly with your model (logic). 您的GUI(视图)不应直接与模型(逻辑)进行交互。 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. 实现一个控制器,该控制器将从您的视图中获取“信号”,并负责创建“逻辑对象”并与之配合使用。

I would instantiate the Logic class outside the GUI, but pass it as an argument to the GUI constructor. 我将在GUI外部实例化Logic类,但将其作为参数传递给GUI构造函数。 It's nearly equivalent to what you are already doing, but I think it makes it clearer that the GUI uses a Logic object. 它几乎等同于您已经在做的事情,但是我认为它使GUI使用Logic对象更加清楚。 Also, it's possible that Logic does some other things before/after the GUI starts/closes; 另外,在GUI启动/关闭之前/之后,Logic可能还会做一些其他事情; 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. 许多其他答案告诉您要查看MVC,但这可能对您的项目而言过于矫kill过正。 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);
...

您可以在主对象中创建Logic类型的on对象,并将该对象的引用存储在Window对象中-这样,只要窗口存在,就可以访问Logic对象。

对于这种琐碎的场景,您应该查找Singleton设计模式

By default, Java uses Reference variables. 默认情况下,Java使用引用变量。 Hence, if you instantiate your object in GUI class, make sure you send the object via method calls to your processing class. 因此,如果在GUI类中实例化对象,请确保通过方法调用将对象发送到处理类。

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. 在该类中,实例化全局所需的所有对象,然后在整个程序中重新使用该实例。

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 ) (根据您的昵称,我将冒险提出其他链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM