what is context object design pattern?

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.


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.


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