Skip to main content

Context object

CreationalAbout 2 min

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 Patternsopen in new window 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.

public class ServiceContext {

    String ACCOUNT_SERVICE, SESSION_SERVICE, SEARCH_SERVICE;

    public void setACCOUNT_SERVICE(String ACCOUNT_SERVICE) {
        this.ACCOUNT_SERVICE = ACCOUNT_SERVICE;
    }

    public void setSESSION_SERVICE(String SESSION_SERVICE) {
        this.SESSION_SERVICE = SESSION_SERVICE;
    }

    public void setSEARCH_SERVICE(String SEARCH_SERVICE) {
        this.SEARCH_SERVICE = SEARCH_SERVICE;
    }

    public String getACCOUNT_SERVICE() {
        return ACCOUNT_SERVICE;
    }

    public String getSESSION_SERVICE() {
        return SESSION_SERVICE;
    }

    public String getSEARCH_SERVICE() {
        return SEARCH_SERVICE;
    }
    
    public String toString() { return ACCOUNT_SERVICE + " " + SESSION_SERVICE + " " + SEARCH_SERVICE;}
}

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

public class ServiceContextFactory {

    public static ServiceContext createContext() {
        return new ServiceContext();
    }
}

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.

public class LayerA {

    private static ServiceContext context;

    public LayerA() {
        context = ServiceContextFactory.createContext();
    }

    public static ServiceContext getContext() {
        return context;
    }

    public void addAccountInfo(String accountService) {
        context.setACCOUNT_SERVICE(accountService);
    }
}

public class LayerB {

    private static ServiceContext context;

    public LayerB(LayerA layerA) {
        this.context = layerA.getContext();
    }

    public static ServiceContext getContext() {
        return context;
    }

    public void addSessionInfo(String sessionService) {
        context.setSESSION_SERVICE(sessionService);
    }
}

public class LayerC {

    public static ServiceContext context;

    public LayerC(LayerB layerB) {
        this.context = layerB.getContext();
    }

    public static ServiceContext getContext() {
        return context;
    }

    public void addSearchInfo(String searchService) {
        context.setSEARCH_SERVICE(searchService);
    }
}

Here is the context object and layers in action.

var layerA = new LayerA();
layerA.addAccountInfo(SERVICE);
LOGGER.info("Context = {}",layerA.getContext());
var layerB = new LayerB(layerA);
layerB.addSessionInfo(SERVICE);
LOGGER.info("Context = {}",layerB.getContext());
var layerC = new LayerC(layerB);
layerC.addSearchInfo(SERVICE);
LOGGER.info("Context = {}",layerC.getContext());

Program output:

Context = SERVICE null null
Context = SERVICE SERVICE null
Context = SERVICE SERVICE SERVICE

Class diagram

alt text
Context object

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