Browser docs

Context object

Name / classification

Context Object

Also known as

Context, Encapsulate Context

Intent

Decouple data from protocol-specific classes and store the scoped data in an object independent of the underlying protocol technology.

Explanation

Real-world example

This application has different layers labelled A, B and C with each extracting specific information from a similar context for further use in the software. Passing down each pieces of information individually would be inefficient, a method to efficiently store and pass information is needed.

In plain words

Create an object and store the data there and pass this object to where it is needed.

Core J2EE Patterns says

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

Programmatic Example

We define what data a service context object contains.

 1public class ServiceContext {
 2
 3    String ACCOUNT_SERVICE, SESSION_SERVICE, SEARCH_SERVICE;
 4
 5    public void setACCOUNT_SERVICE(String ACCOUNT_SERVICE) {
 6        this.ACCOUNT_SERVICE = ACCOUNT_SERVICE;
 7    }
 8
 9    public void setSESSION_SERVICE(String SESSION_SERVICE) {
10        this.SESSION_SERVICE = SESSION_SERVICE;
11    }
12
13    public void setSEARCH_SERVICE(String SEARCH_SERVICE) {
14        this.SEARCH_SERVICE = SEARCH_SERVICE;
15    }
16
17    public String getACCOUNT_SERVICE() {
18        return ACCOUNT_SERVICE;
19    }
20
21    public String getSESSION_SERVICE() {
22        return SESSION_SERVICE;
23    }
24
25    public String getSEARCH_SERVICE() {
26        return SEARCH_SERVICE;
27    }
28    
29    public String toString() { return ACCOUNT_SERVICE + " " + SESSION_SERVICE + " " + SEARCH_SERVICE;}
30}

Create an interface used in parts of the application for context objects to be created.

1public class ServiceContextFactory {
2
3    public static ServiceContext createContext() {
4        return new ServiceContext();
5    }
6}

Instantiate the context object in the first layer and the adjoining layer upcalls the context in the current layer, which then further structures the object.

 1public class LayerA {
 2
 3    private static ServiceContext context;
 4
 5    public LayerA() {
 6        context = ServiceContextFactory.createContext();
 7    }
 8
 9    public static ServiceContext getContext() {
10        return context;
11    }
12
13    public void addAccountInfo(String accountService) {
14        context.setACCOUNT_SERVICE(accountService);
15    }
16}
17
18public class LayerB {
19
20    private static ServiceContext context;
21
22    public LayerB(LayerA layerA) {
23        this.context = layerA.getContext();
24    }
25
26    public static ServiceContext getContext() {
27        return context;
28    }
29
30    public void addSessionInfo(String sessionService) {
31        context.setSESSION_SERVICE(sessionService);
32    }
33}
34
35public class LayerC {
36
37    public static ServiceContext context;
38
39    public LayerC(LayerB layerB) {
40        this.context = layerB.getContext();
41    }
42
43    public static ServiceContext getContext() {
44        return context;
45    }
46
47    public void addSearchInfo(String searchService) {
48        context.setSEARCH_SERVICE(searchService);
49    }
50}

Here is the context object and layers in action.

1var layerA = new LayerA();
2layerA.addAccountInfo(SERVICE);
3LOGGER.info("Context = {}",layerA.getContext());
4var layerB = new LayerB(layerA);
5layerB.addSessionInfo(SERVICE);
6LOGGER.info("Context = {}",layerB.getContext());
7var layerC = new LayerC(layerB);
8layerC.addSearchInfo(SERVICE);
9LOGGER.info("Context = {}",layerC.getContext());

Program output:

1Context = SERVICE null null
2Context = SERVICE SERVICE null
3Context = SERVICE SERVICE SERVICE

Class diagram

alt text

Application

Use the Context Object pattern for:

  • Sharing information across different system layers.
  • Decoupling software data from protocol-specific contexts.
  • Exposing only the relevant API’s within the context.

Known uses

Credits