如何学习“关注点分离”在java中
在另一个问题中,有人告诉我在我的java程序中实现以下内容。但是,我对 Java 很陌生,我不知道如何开始将我的简单程序转换为这种结构:
Data Access Layer (read/write data)
Service Layer (isolated business logic)
Controller (Link between view and model)
Presentation (UI)
dependency injection.
program to the interface:
它是否存在于某些框架内?我应该开始学习Spring,这个结构就会自然演变吗?或者说,我是否可以在不使用框架的情况下将上述技术一一实现?
In another question, someone told me to implement the following in my java program. But, I am very new to Java and I do not know how to start to convert my simple program into this structure:
Data Access Layer (read/write data)
Service Layer (isolated business logic)
Controller (Link between view and model)
Presentation (UI)
dependency injection.
program to the interface:
Does that come inside some framework? Should I start learning Spring and this structure will evolve naturally? Or, can I implement above technologies one by one without using a framework?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您愿意,您可以在没有框架的情况下实现它们,但您会放弃框架为您提供的任何好处。
您引用的分层是正确的并且独立于任何框架;这只是接口编程和关注点分离。如果您希望最大程度地减少现在想要学习的新技术的数量,那么您可以在没有 Spring 的情况下自由地做到这一点。
如果你不知道什么是持久,那么你就不应该跳入Spring。对于大多数人来说,持久性意味着使用 SQL 将数据存储在关系数据库中。如果您不知道,我建议您从这里开始。
如果您从未使用过底层技术,世界上所有的模式书籍都无法帮助您。
如果您从未做过这些,我建议您坚持使用直接的 JDBC、Servlet 和 JSP,仅使用 JSTL(无 scriptlet)。除此之外的任何事情都会令人困惑。
如果您有一个 Foo 模型对象,具有持久性、服务和视图层,那么接口可能如下所示:
当然,这些只是接口。您需要提供实现。
You can implement them without a framework if you wish, but you give up whatever benefits the framework offers you.
The layering you cite is correct and independent of any framework; it's just programming to interfaces and separation of concerns. You're free to do it without Spring if you wish to minimize the number of new technologies you want to learn right now.
If you don't know what persistence is, then you shouldn't jump into Spring. Persistence means storing data in relational databases using SQL to most people. If you don't know that, I'd recommend starting there.
All the patterns books in the world won't help you if you've never used the underlying technologies.
If you've never done any of this, I'd recommend sticking to straight JDBC, servlets, and JSPs using only JSTL (no scriptlets). Anything beyond that will just be confusing.
If you had a Foo model object, with persistence, service, and view tiers, the interfaces might look like this:
These are just interfaces, of course. You'll need to provide implementations.
您可能需要查看域驱动设计。代码示例采用 Java 语言。您列出的内容与设计相关,而不是任何特定技术。
You might want to check out Domain Driven Design. The Code samples are in Java. The things you listed are design related more than any specific technology.
简而言之:
例如,注销按钮控制器将请求注销操作/服务以使用户登录的所有服务上的用户会话无效,然后它将选择适当的视图 或注销网页以将用户转发到。
Spring 是依赖注入的一个很好的实现。您可能不想编写自己的框架,在这个阶段,您不应该这样做。有一个 Spring MVC 框架 可以做一些事情为你。
但我建议你从非常基础的开始。不要跳入术语,而是从基础开始阅读。从一本关于使用 Java 进行应用程序开发的好书开始。您还可以查看
In short:
For example, a sign-off button controller will request a sign-off action/service to invalidate user's sessions on all services that user is logged on to, then it will choose an appropriate view or log-off web-page to forward user to.
Spring is a good implementation of dependency injection. You may not want to write your own framework, and at this stage, you should rather not. There is a Spring MVC framework that can do things for you.
But I suggest you start from very basic. Instead of jumping on jargon, read from basic. Start with a good book on application development using Java. You can also look into
您可以根据自己的需要实现所有这些——以前已经做过很多次了,但没有什么可以阻止您再次这样做。
更好地利用您的时间是确保您了解上面列出的关注点分离(通常是正确的)并确定要利用的现有框架的最有效集成(例如 Hiberante、Spring、Guice 等) 。对于这个问题有多种答案(而且不乏意见!),但在所有条件相同的情况下,您需要集成的框架越少,它可能就越容易、越适合。
Spring 有一个非常知名的框架,涵盖了其中的许多内容,因此从那里开始是明智的。它还允许您与其他框架一起工作(即,您可以使用 Spring 的选择性部分)。例如,您可以使用 Spring 进行依赖注入并使用不同的 MVC 框架。
You can implement all of this is you want -- it's been done many times before, but nothing prevents you from doing it again.
What would be a better use of your time is to make sure you understand the separation of concerns you listed above (which are generally right) and identify the most efficient integration of existing frameworks to leverage (e.g., Hiberante, Spring, Guice, etc). There are multiple answers for that one (and no shortage of opinions!), but all things being equal, the less frameworks you have to integrate, the easier and better fitting it's likely to be.
Spring has a very well known framework which covers many of these things, so it would be wise to start there. It also allows you to work with other frameworks (i.e., you can use selective parts of Spring). For example, you can use Spring for dependency injection and use a different MVC framework.
这个问题很难回答。首先,我不知道你的程序是什么样的。其次,我认为“转换”它不是可以做的事情,或者应该为此做的事情。您所说的是开发人员在设计应用程序时通常会想到的架构概念。
如果您对这些概念感兴趣,我建议您阅读一些有关 模型的内容-视图-控制器模式(MVC)和面向服务的架构(SOA)。
这些是一般概念,并不专门适用于 Java。然而,它们广泛应用于Java企业开发中。各种框架允许您利用这些概念创建应用程序。例如,正如其他人指出的那样,Spring Web MVC 是 Spring 框架的一部分,可让您创建遵循 MVC 模式的 Web 应用程序。
It is very hard to answer this question. First of all, I don't know what your program looks like. Second, I don't think 'converting' it is something that can be done, or should be done for that matter. What you're talking about are architectural concepts that the developers usually have in mind while designign the application.
If these concepts interest you, I suggest reading a bit about Model-View-Controller pattern (MVC) and service-oriented Architecture (SOA).
These are general concepts that do not apply specifically to Java. However, they are widely used in Java enterprise development. Various frameworks allow you to create applications utilizing these concepts. For example, Spring Web MVC, as others have pointed out, is part of the Spring Framework that lets you create web applications that adhere to the MVC pattern.
如果您的程序非常简单,则可以通过为每个程序使用一个调用来完成这种分离
类别。
如果它不是那么简单的程序,您可能需要为每个类别提供一个包。
但是 - 不要过度设计!这些概念旨在帮助您管理大型应用程序,而不是毁掉您的编程入门!
If your program is really simple this separation might be done by using one calss for each
category.
if it's not as simple program you might want to have package for each category.
BUT - don't overdesign! These concepts are ment to help you manage large scale applications, not to ruin you in your programming begginigs!