gpt4 book ai didi

java - 如何在java中学习 "separation of concern"

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:39:12 24 4
gpt4 key购买 nike

在另一个问题中,有人告诉我在我的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,并且这种结构会自然发展?或者,我可以不使用框架,一个一个实现上面的技术吗?

最佳答案

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

您引用的分层是正确的并且独立于任何框架;它只是针对接口(interface)编程和关注点分离。如果您希望尽量减少现在想要学习的新技术的数量,您可以在没有 Spring 的情况下自由学习。

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

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

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

如果您有一个 Foo 模型对象,带有持久层、服务层和 View 层,接口(interface)可能如下所示:

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);
}

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

关于java - 如何在java中学习 "separation of concern",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4759070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com