Use the constructor design pattern to create and initialise the data in your business objects in a consistent fashion and in one single place with a reusable component model within a Process Application or Toolkit. Adopt this design approach, instead of having multiple process code locations for initialising and setting default values for Business Objects. Typically, Business Object initialisation is done using local private JavaScript blocks which make the code difficult to manage, update and leads to errors/defects that difficult to trace and fix.

This pattern is a specialisation of the standard Singleton Design pattern.

The main parts of this pattern are:

Define a Business Object variable
Create a general system service to be used as the singleton constructor
Declare the target Business object as an input, output and private variable.
Model the General System Service
Create an initialisation wrapper service that calls the constructor

The example below creates a constructor for a list of Job Business Object.

Define a Business Object variable

In this case, I’m using a complex variable called Job
The Business object consists of three parameters – JobDescription, JobStatus, and JobCompleted

BOdetails

Create a general system service to be used as the singleton constructor

Create a service of type “General System Service” that will be used to instantiate the Business Object and set default values, if required.

I’ve used the prefix underscore (_) in the name to indicate this service is a helper or utility service that should only be used within other services and never directly in a human service or BPD activity. This makes it easy to spot misuse and ensure process developers are using agreed design guidelines. Also make it easier to unit test services based on different implementation and usage types.

Created _constructorListOfJobs

constructorService-01Overview

Declare the target Business object as an input, output and private variable.

If your variable is a list also declare as an input an integer for the size of the list to be created and populated with default values.

The input and output variable should be the same Business Object type and have a descriptive name.
The private variable should be the same Business Object type as the input and output variables, called something like “_constructor”, using a naming convention to indicate this is only ever to be a private variable.
Select the “Has Defaults” check box for the private variable to auto-generate the script to create and initialise the Business object.

See screenshots below for details.

constructorService-02Variables


constructorService-02Variables1

Model the General System Service

Create a script block that contains the default values for this type of Business Object.
In addition, you should add error handling and logging to catch any errors. Typical errors would be initialisation script errors occurring after the modification of the Business Object, for example renaming a parameter.
Usually, the “Has Defaults” auto generated script should update in synch with any changes in the Business object. If it does not for whatever reason, just uncheck and recheck the check box to correct the “Has Defaults” auto generated script.

If required, make the necessary changes to the default values initialisation script block. Now any referencing BPD or Service that requires a variable of this Business Object type to be created, initialised and possibly have default values set will automatically have the benefit of these changes.

Always nullify private variables for efficient memory management.

I’ve used two exits as this is consistent with the developer guidelines agreed on the project for modelling utility and helper services. This simplifies the models in parent services and makes the flow logic clear when a system error, as opposed to business exception, has occurred.

constructorService-03Constructor

Create an initialisation wrapper service that calls the constructor

Create a wrapper service that encapsulates and calls the constructor. This also has the benefit of being able to use the “Where Used” feature on the modular wrapper service to clearly see references to other services and BPDs using this particular Business Object initialisation logic, instead of trawling through individual private JavaScript blocks.

constructorService-04ConstructorWrapper


constructorService-04ConstructorWrapper1