简体   繁体   中英

what is context object design pattern?

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

A Context is a collection of data, often stored in a Map or 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 Context pattern as an anti-pattern .

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.

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

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.

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

For more detail see this article

"Context" comes from "Contextual Information".

From 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM