Announcing Stack Overflow Documentation



Sign up and start helping → Learn more about Documentation →

what is context object design pattern ?

share|improve this question
If you provided some background information as to why you are considering this pattern you may receive a more positive response for people as it will invoke more interest. – David Robbins Jun 13 '09 at 17:26
up vote 23 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

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

share|improve this answer
This is too detailed. You are maybe thinking of HttpContext, not a context in general. – Slavo Jun 20 '14 at 14:36

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

share|improve this answer

See Can you explain the Context design pattern a bit? 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.

share|improve this answer

Your Answer


