IBM®
Skip to main content
    Country/region [select]      Terms of use
 
 
    
     Home      Products      Services & solutions      Support & downloads      My account     
 
developerworks > Dashboard > Bobby Woolf: WebSphere SOA and J2EE in Practice > Miscellaneous Technologies > Context-Free Methods
developerWorks
Log In   View a printable version of the current page.
Overview Spaces Forums Blogs Podcasts Wikis Exchange
Context-Free Methods
Added by bwoolf, last edited by bwoolf on Oct 10, 2006
Labels: 
(None)

Context-Free Methods

The most reusable code is functionality that can be wrapped up as context-free methods. A context-free object is one whose public API is all context-free methods. These serve as the basis for implementing stateless session beans and stateless services.

I prefer the term "context-free" to the term "stateless" because all code has state. The state unique to a particular object depends on what methods have been run; context-free objects don't have this dependency on what's been done in the past. What makes code "stateless" is that it doesn't know what context it's in; it has no session tracking conversational state, it has no history of what has happened before it was called. Any necessary context is passed in from the caller. It has state, but the state is global to the process and available equally to any of the code, such as a database.

Objects can have state. It's stored in instance variables, which are scoped to the object's lifetime. These variables retain their values between method invocations, which is context that makes the object stateful. So the main trick for making an object, and thus its methods, context-free is for the object to not have instance variables.

Technically, an object can have instance variables and still be context-free as long as:

  1. The variable's value is initialized when the object is created and then doesn't change during the (rest of the) object's lifetime.
  2. Different instances of the same class have instance variable values that are either the same or equivalent.

For example, a context-free object may have instance variables whose values are initilized from a properties file when the object is created. All instances will have the same values, and so those values represent configuration, not context.

A context-free object can use stateful objects as long as it creates them. Such an object's lifetime will end when the conext-free method that created it ends. A context-free object can also retrieve stateful objects using the context passed in from the caller, such as an object identifier.

To summarize, a context-free method has only two kinds of state:

  1. Global state that is available to any code in the process, such as a database
  2. Context that is passed in from the function's caller as parameters

A context-free object has either no or equivalent instance state so that any instance is equivalent to any other. It's public API is context-free methods.


    About IBM Privacy Contact