Java 什么是上下文对象设计模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/771983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:18:08  来源:igfitidea点击:

what is context object design pattern?

javadesign-patterns

提问by Tony

what is context object design pattern ?

什么是上下文对象设计模式?

采纳答案by akf

A Context is a collection of data, often stored in a Mapor in a custom class which acts as a struct with accessors and modifiers. It is used for maintaining state and for sharing information within a system. See this PDF for an indepth description. Though it can be used for efficient and effective data sharing, you should note that many are wary of the Contextpattern as an anti-pattern.

上下文是数据的集合,通常存储在Map自定义类中,该类充当具有访问器和修饰符的结构。它用于维护状态和在系统内共享信息。有关详细说明,请参阅此 PDF。虽然它可以用于高效和有效的数据共享,但您应该注意,许多人都对Context作为反模式的模式持谨慎态度。

回答by Tamas Rev

An example for it might be the HttpSession object: you have attributes which is basically a map with String keys and Object elements. This provides state information between http requests. Another example is the ServletRequest which provides state information between Servlets.

一个例子可能是 HttpSession 对象:你有属性,它基本上是一个带有字符串键和对象元素的映射。这提供了 http 请求之间的状态信息。另一个例子是 ServletRequest,它提供 Servlet 之间的状态信息。

回答by Abdur Rahman

A context object encapsulates web server specific HTTP information in more general and portable form.

上下文对象以更通用和可移植的形式封装特定于 Web 服务器的 HTTP 信息。

It is used when:

它在以下情况下使用:

  • you have components and services that need access to the system information.
  • You want to decouple application components and services from the protocol specifics of system information.
  • you want to expose only relevant APIs within a context.
  • 您有需要访问系统信息的组件和服务。
  • 您希望将应用程序组件和服务与系统信息的协议细节分离。
  • 您只想在上下文中公开相关的 API。

Use a context object to encapsulate state in a protocol-independent way to be shared throughout your application.

使用上下文对象以独立于协议的方式封装状态,以便在整个应用程序中共享。

For more detail see this article

更详细的看这篇文章

回答by 8bitjunkie

"Context" comes from "Contextual Information".

“语境”来自“语境信息”。

From Msu.edu:

来自 Msu.edu:

Contextual Information: Information that is in addition to the actual text of the document, such as date sent, sender's identity, addressee's identity, routing information, and return receipts.

上下文信息:除文档实际文本之外的信息,例如发送日期、发件人身份、收件人身份、路由信息和回执。

In most frameworks (and thus the design pattern), a Context Object stores information about an incoming request and corroborating a response.

在大多数框架(以及设计模式)中,上下文对象存储有关传入请求和确认响应的信息。

回答by Glen Best

See Can you explain the Context design pattern?for a thorough description of the pattern.

请参阅您能解释上下文设计模式吗?模式的详细描述。

My post there also explains why it's not an anti-pattern - but the botched implementation of it is.

我在那里的帖子还解释了为什么它不是反模式 - 但它的拙劣实现是。