简体   繁体   English

如何在Java项目中管理适当的结构

[英]How to manage a proper structure in a java project

I have encountered with this problem often. 我经常遇到这个问题。 Couldn't think of a way to how to face this. 想不出一种方法来面对这个问题。 Now I got this excellent forum!! 现在我有了这个很棒的论坛! I'm sure experts here will help me with this. 我敢肯定,这里的专家会为我提供帮助。

This is not a problem about a specific code segment like thing. 这不是关于特定代码段之类的问题。 I am capable (as I think) of doing some advance projects in java. 我有能力(按照我的想法)在Java中进行一些高级项目。 It means, simply, I can finish the project to give the exact result. 简而言之,这意味着我可以完成项目以给出确切的结果。

But the problem is, though I can finish it some how, I'm not satisfied with the management of the classes etc. Actually I don't know how to properly manage it. 但是问题是,尽管我可以用某种方式完成它,但是我对类的管理不满意。实际上我不知道如何正确地管理它。 I'll explain considering the project I'm currently working on (I'm not a professional coder, this will be for my self learning). 我将解释考虑当前正在从事的项目(我不是专业编码人员,这是我的自学知识)。

I'm working on a database management system (MySQL + Java). 我正在使用数据库管理系统(MySQL + Java)。 There I'm hoping to implement several features there. 我希望在那里实现一些功能。 The flow will be, roughly, like this. 流程大致是这样的。

1. Login
2. Main window

Main window will be like this. 主窗口将是这样。

在此处输入图片说明

In the left panel you can select what you need to do. 在左侧面板中,您可以选择需要执行的操作。

Eg.

*. Add some entries to the database

*. Search database

*. Other..(will be add later)

Can some one please tell me how to manage all those things, two frames and several panels. 有人可以告诉我如何管理所有这些东西吗?两个框架和几个面板。

What I've done is like this. 我所做的就是这样。

I've written a managerClass which has the main method. 我写了一个具有main方法的managerClass Then It'll call loginFrame first. 然后, loginFrame首先调用loginFrame After validation, loginFrame calls a method in managerClass , to load mainFrame . 验证之后, loginFrame调用的方法managerClass ,加载mainFrame But I keep a reference to the managerClass in all the frames, panels etc.. as I save all the required info in managerClass , like user name ect.. 但是我在所有框架,面板等中都保留了对managerClass的引用。因为我将所有必需的信息都保存在managerClass ,例如用户名等。

But when modifying and debugging, things become really really difficult. 但是,在修改和调试时,事情变得非常困难。 As I haven't done thing according to any specific rule. 因为我还没有根据任何特定规则进行操作。 I'll have to modify almost all the classes to do a slight modification. 我将不得不修改几乎所有的类来进行一些小的修改。 I've though a lot and sea 我虽然海很多

From what I can understand of your application, your main issue is the coupling between your components and the different layers of the application (presentation, interaction control, domain logic). 据我对您的应用程序的了解,您的主要问题是组件与应用程序不同层(表示,交互控制,域逻辑)之间的耦合。 I would suggest using two design patterns (or styles) that may help here: 我建议使用两种设计模式(或样式),可能会在这里有所帮助:

  1. Model-View-Presenter: separates the Model/Domain logic from the presentation (View) from the logic of controlling user interactions. Model-View-Presenter:将模型/域逻辑与表示(视图)与控制用户交互的逻辑分开。 I've used it recently in a large application and I've found it really helps to separate concerns in a clean way and makes testing much more simple. 我最近在大型应用程序中使用了它,发现它确实有助于以一种干净的方式分离问题,并使测试变得更加简单。 Please don't confuse it with the Model-View-Controller model, which is close, but have many issues. 请不要将其与Model-View-Controller模型混淆,该模型很接近,但是存在很多问题。 There's a nice article by Martin Fowler describing these two patterns Martin Fowler有一篇不错的文章描述了这两种模式
  2. Adopt an event based interaction between your components. 在组件之间采用基于事件的交互 Instead of the Login frame calling the Manager class, make it fire an event ("user authenticated"), which is handle by the interested components. 代替Login框架调用Manager类,而是使它触发一个事件(“用户身份验证”),该事件由感兴趣的组件处理。 For example, you may have a small user account details panel in the main window which is not displayed until this event is triggered. 例如,您可能在主窗口中有一个小的用户帐户详细信息面板,直到触发此事件后才会显示。 Unfortunately, I'm not aware of any event framework for plain Java (which I suspect if the what you are using for development) The one I use is for Google Web Toolkit. 不幸的是,我不知道任何用于纯Java的事件框架(我怀疑它是否用于开发)。我使用的是Google Web Toolkit。

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

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