编程中的服务类别是什么? [英] What is the service class in programming?

查看:342
本文介绍了编程中的服务类别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是看到一些名为"ClassNameService"的类,那么逻辑上有什么区别?这些服务类别的目标是什么?

I always see some classes that is named "ClassNameService", well what is the difference as logic? What is the goal of these service classes?

推荐答案

通常来说,可能存在由服务控制的域对象层次结构.如果这些域对象只是数据占位符,没有任何行为,那么面向对象编程就不正确.

Generally speaking, there could be a hierarchy of domain objects, which are controlled by the services. If these domain objects are only data placeholders, with no behavior, then this is not true to object-oriented programming.

我们在这里所拥有的就是马丁·福勒(Martin Fowler)所说的贫血域模型.

What we have here is what Martin Fowler would call the Anemic Domain Model.

更常见的是,在OOP中,一组域对象具有行为,这些行为的相互作用形成了业务逻辑.反过来,此逻辑由 Service 封装.

More commonly, within OOP, a group of domain objects have behavior whose interactions form business logic. This logic, in turn, is encapsulated by the Service.

此类服务是有状态的,其状态由这些域对象组成. 服务也可能无状态并提供自给自足的功能.

Such services are stateful, with their state being comprised of these domain objects. Services may also be stateless and offer self-sufficient functionality.

想象一下,如果您愿意的话,可以使用一个非常简单的计算器API.

Imagine, if you will, a very simple calculator API.

已向您的应用程序发送了一个HTTP请求,该应用程序使用API​​进行数据提取,进行一些复杂的计算,最后使用HTTP响应将包含已计算的数据作为SOAP/REST/etc.来自此端点的消息.

An HTTP request was sent your application, which the uses the API in data extraction, some complex calculation, and, at the end, an HTTP response containing the computed data as a SOAP/REST/etc. message from this endpoint.

一旦收到响应,则应将其返回给发送原始请求的客户端.

Once the response is received, this should then be returned to the client that sent the original request.

您不想强迫客户端手动调用输入的计算和转换.相反,您只想向他们提供服务API ,该API封装了此逻辑并向他们返回了预期的结果.

You don't want to force your client to manually invoke the computation and transformation of the input. Instead, you want to simply offer them a service API which encapsulates this logic and returns to them the expected result.

对于Spring应用程序,您具有Spring注释

For Spring applications, you have the Spring annotation @Service.

这篇关于编程中的服务类别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆