Java “如何学习”;“关注点分离”;在爪哇

Java “如何学习”;“关注点分离”;在爪哇,java,design-patterns,frameworks,Java,Design Patterns,Frameworks,在另一个问题中,有人告诉我在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:

在另一个问题中,有人告诉我在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吗?这种结构会自然演变吗?或者,我可以在不使用框架的情况下逐个实现上述技术吗?

您可能想查看一下。代码示例是用Java编写的。您列出的内容与设计相关,而不是与任何特定技术相关

如果您愿意,您可以在没有框架的情况下实现它们,但是您放弃了框架为您提供的任何好处

您引用的分层是正确的,与任何框架无关;它只是编程接口和分离关注点。如果您希望减少您现在想要学习的新技术的数量,那么您可以在没有Spring的情况下自由地使用它

如果你不知道什么是持久性,那么你就不应该跳进春天。对于大多数人来说,持久性意味着使用SQL将数据存储在关系数据库中。如果你不知道,我建议从那里开始

如果您从未使用过底层技术,那么世界上所有的模式书籍都不会对您有所帮助

如果您从未这样做过,我建议您只使用JSTL(无Scriptlet)直接使用JDBC、servlet和JSP。除此之外的任何事情都会令人困惑

如果您有一个具有持久性、服务和视图层的Foo模型对象,则接口可能如下所示:

package model;

/**
 * A model object that's interesting from your problem's point of view
 */
public class Foo
{
}

package persistence;

/**
 * CRUD operations for a Foo 
 */
public interface FooDao
{
    Foo find(Long id);
    List<Foo> find();
    void saveOrUpdate(Foo foo);
    void delete(Foo foo);
}


package service;

/**
 * Just a data service that wraps FooDao for now, but other use cases would 
 * mean other methods.  The service would also own the data connection and manage 
 * transactions.
 */
public interface FooService
{
    Foo find(Long id);
    List<Foo> find();
    void saveOrUpdate(Foo foo);
    void delete(Foo foo);
}

package view;

/**
 * A class that owns services, validates and binds input from UI, and handles routing 
 * to the next view once service is complete.
 */
public interface FooController
{
   ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response);    
}
包模型;
/**
*从问题的角度来看很有趣的模型对象
*/
公开课Foo
{
}
包持久性;
/**
*Foo的积垢操作
*/
公共接口FooDao
{
Foo-find(长id);
列表查找();
作废保存或更新(Foo-Foo);
作废删除(Foo-Foo);
}
一揽子服务;
/**
*现在只是一个包装FooDao的数据服务,但其他用例会
*指其他方法。该服务还将拥有数据连接并管理数据
*交易。
*/
公共接口服务
{
Foo-find(长id);
列表查找();
作废保存或更新(Foo-Foo);
作废删除(Foo-Foo);
}
包视图;
/**
*拥有服务、验证和绑定UI输入以及处理路由的类
*服务完成后,转到下一个视图。
*/
公共接口控制器
{
ModelAndView HandlerRequest(HttpServletRequest请求、HttpServletResponse响应);
}

当然,这些只是接口。您需要提供实现。

您可以按照自己的意愿实现所有这一切——以前已经做过很多次了,但没有什么可以阻止您再次这样做

更好地利用您的时间是确保您理解上面列出的关注点的分离(通常是正确的),并确定要利用的现有框架的最有效集成(例如Hibernate、Spring、Guice等)。对于这一点,有多种答案(并不缺少意见!),但在所有条件相同的情况下,您需要集成的框架越少,就越容易和更好地适合它


Spring有一个非常著名的框架,涵盖了许多这些内容,因此从这里开始是明智的。它还允许您使用其他框架(即,您可以使用Spring的某些部分)。例如,您可以使用Spring进行依赖项注入,并使用不同的MVC框架。

很难回答这个问题。首先,我不知道你的程序是什么样子的。第二,我不认为“转化”是可以做的,或者应该做的事情。您所说的是开发人员在设计应用程序时通常想到的体系结构概念。
如果你对这些概念感兴趣,我建议你读一读关于和的书

这些是一般概念,并不专门适用于Java。然而,它们在Java企业开发中被广泛使用。各种框架允许您利用这些概念创建应用程序。例如,正如其他人所指出的,SpringWebMVC是Spring框架的一部分,它允许您创建遵循MVC模式的Web应用程序

如果您的程序非常简单,那么可以通过为每个程序使用一个CALS来完成此分离 类别

Data Access Layer (read/write data) -> one class for presisting laoding
Service Layer (isolated business logic) -> one calss with bussiness logic
Controller (Link between view and model) -> in simple swing app this merges with UI
Presentation (UI) -> one class for one widnow
dependency injection -> not used in small apps 
program to the interface -> Your service class should use interface tah is used by other class instead of directly your serivce implementation:
若它不是那个么简单的程序,那个么你们可能需要为每个类别设置一个包

Data Access Layer (read/write data) -> one class for presisting laoding
Service Layer (isolated business logic) -> one calss with bussiness logic
Controller (Link between view and model) -> in simple swing app this merges with UI
Presentation (UI) -> one class for one widnow
dependency injection -> not used in small apps 
program to the interface -> Your service class should use interface tah is used by other class instead of directly your serivce implementation:
但是-不要过度设计!这些概念旨在帮助您管理大规模应用程序,而不是在编程过程中毁掉您

简而言之: