Advertisement Jump to content
  • Advertisement
  • 06/19/01 11:39 PM
    Sign in to follow this  

    Context/Virtual Self

    General and Gameplay Programming

    Myopic Rhino

    Sharing state data between multiple objects, either to implement a finite state machine or to compose multiple objects to perform a particular task.


    Sharing data between multiple objects that have only one instance is pretty easy. Global data or singletons are easy solutions. However, if you want to compose discrete objects into a single component, any sort of global access pattern fails to scale to a finer level.


    Create an object Context that will hold any state data and methods relevant to the operation of the component. Add this object as a parameter or member pointer to every class the component will create and use. Have the component create and own the Context object. Using Context as an extra parameter provides more flexibility with respect to usage, as it allows multiple components to rely on a single object for part of their implementation.


    See the State Pattern in Design Patterns for an implementation of the Context pattern.

    • Easier development of components that are a composition of smaller objects.
    • Extra overhead, either as an extra parameter to every method call or an extra member pointer.
    • Using the Context object as an extra parameter allows stateless objects (i.e. the Context object contains all relevant state information) to be implemented as Flyweights, obviating the need for multiple instances of lightweight objects.
      [size="5"]Related Patterns

      State uses a Context to pass state information between various states. Context can be thought of as a Singleton with finer levels of access beyond global.

      Report Article
    Sign in to follow this  

    User Feedback

    There are no comments to display.

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!