{title:'General Design', created:'9.0.0'}

The Juneau framework uses the design pattern of builders, context, and session objects:

This is a general design pattern used throughout the framework including the REST client and server APIs.

The following shows the general pattern for creating sessions:

| // Create a reusable context object (in this case a serializer). | WriterSerializer serializer = JsonSerializer | .create() // Instantiates a context builder. | .findFluentSetters() // Sets a configuration value. | .build(); // Creates a context. | | // Create a one-time session object. | WriterSerializerSession session = serializer | .createSession() // Instantiates a session builder. | .useWhitespace() // Sets a session value. | .build(); // Creates a session. | | // Use it. | String json = session.serialize(myBean);

Typically developers will not deal with session objects and will just use convenience methods on the context classes themselves that handle creation of sessions:

| // Just use serialize method on WriterSerializer class. | String json = serializer.serialize(myBean);

Most context objects also have static default instances that can be used in leu of creating new contexts as well:

| // Just use one of the static context instances. | String json = JsonSerializer.DEFAULT.serialize(myBean);

Most context classes also have the ability to clone and modify existing context objects:

| // Clone and modify an existing context object. | WriterSerializer serializer = JsonSerializer | .DEFAULT | .copy() // Instantiates a context builder. | .findFluentSetters() // Sets a configuration value. | .build(); // Creates a context.

The default values of many context settings can also be set via system properties and environment variables. The javadocs on these settings will identify when this is possible.

The framework makes heavy use of caching of existing context objects with the same builder settings. This is a critical reason why Juneau achieve impressive performance. Using Java reflection to find out all information about a bean type is expensive. By caching context objects, we only need to reflect that bean type once and store that information in the context for reuse by all serializers and parsers that share the same bean context configuration settings.