Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you explain the Context design pattern?

I've started to read about the Context design pattern. Here's what I understood from the text :

  • you have a map containing all your variables

  • you pass it around to whoever needs it, so that you won't have to send all the variables as method parameters

Did I "get" it?

like image 846
Geo Avatar asked Jun 12 '09 14:06

Geo


People also ask

What are design patterns example and explanation?

Design patterns provide a standard terminology and are specific to particular scenario. For example, a singleton design pattern signifies use of single object so all developers familiar with single design pattern will make use of single object and they can tell each other that program is following a singleton pattern.

What is a context object?

The context object is an internal JSON structure that is available during an execution, and contains information about your state machine and execution. This allows your workflows access to information about their specific execution. You can access the context object from the following fields: InputPath. OutputPath.

What is context object in Java?

A Context object contains a list of properties in the form of NamedValue objects. These properties represent information about the client, the environment, or the circumstances of a request and generally are properties that might be inconvenient to pass as parameters.

Is context an Antipattern?

Context is an anti pattern, since it is used as a container for what is unknown a priori.


1 Answers

Did I "get" it?

Sorry to say, not quite.

The goal of Context Object is not to pass lots of parameters to methods implicitly, as a means of by-passing strong typing and encapsulation. The goal is to store scoped data in a general, but managed way, independent of protocols and presentation technology. Data stored within a scope is by nature shared, can still be structured, and is inherently different than one-off parameters passed to a method.

Context Object Pattern was first introduced in the Core J2EE Patterns 2nd Ed. The 'Context' part refers to the fact that the Object holds data in the Context of a scope
(such as application/session/request/conversation/flash).

Its purpose is to decouple, as much as possible, application data and logic from protocol/presentation-technology- specific classes such as HttpSession and HttpRequest.

Pattern Implementation

Under Context Object, data intended for application/session/request/other scope is not put directly into ServletContext/HttpSession/HttpRequest/other protocol-specific class. Instead, the data is stored in a POJO wrapper class, that then sits in the ServletRequest/HttpSession/HttpRequest/other.

The Context Object may store the data in a map, but it doesn't need to - it can store the data in any structure/format relevant to the program.

An application may use one Context Object class per scope, or several classes which split the data in an orderly fashion, avoiding excessive Class bloat and promoting separation of concerns.

The Context Object is used by the frontmost presentation classes (Views, Front Controllers, Dispatchers). These presentation client objects call contextObject.get to retrieve stored scoped data and contextObject.put to store scoped context data.

It is not passed into business/integration logic. It is not used as a means of passing a multitude of parameters into business objects, by-passing strong typing. The Business and Integration Tiers are fronted by Business Delegates, Application Services and/or Session Facades which use specific strongly-typed parameters.

Pattern Benefits

  • Testability: Unit tests only need to mock a simple POJO, rather than a protocol-specific complex server class, such as ServletContext or HttpRequest
  • Flexibility & Reusability: The core of the application works independently of the thin protocol-specific 'presentation' layer of classes. This means that an application can more easily change or add protocols or presentation technology (e.g. HTML/HTTP/Servlet and WAP/Servlet and XML/SOAP/HTTP/EJB and HTML/HTTP/JSF).

Comments

  • Is an historical pattern
  • One could argue that dependency injection frameworks, such as CDI, Guice, Spring, Seam, & others give scope storage already implemented in a protocol-independent way. i.e. that all of the scopes are implemented as Context Objects already, meaning that the developer is less compelled to create additional Context Objects. That doesn't negate the pattern - it means the CDI framework already supports the pattern.
  • If incorrectly implemented, one can end up with the "Pass Around Ginormous Context Objects Throughout The Application" anti-pattern

Quoting KaptajnKold: I think you got it. However, I also think it is more of an anti-pattern to be avoided. See why here.

Your comments refer to the misimplemented version of Context Object. Context Object itself is not an anti-pattern.

like image 198
Glen Best Avatar answered Oct 07 '22 14:10

Glen Best