Overview

This page should try to explain a way to enforce encapsulation in a Teamworks Variable Type. If you are not familiar with encapsulation I suggest you read the “Encapsulation (object oriented programming)” article on wikipedia.

In Teamworks you define Variable Types which are accessed using Javascript. The use of Javascript and the lack of a global Javascript context throughout your process makes it difficult to enforce encapsulation on objects of your Variable Types.

The benefits of encapsulation are:

“A benefit of encapsulation is that it reduces system complexity and thus increases robustness, by limiting the inter dependencies between software components.” [Wikipedia]
You can find out who and what are making changes to your objects variable properties.

I came up with a design approach that tries to implement encapsulation in Teamworks. It makes use of the following artifacts:

Initializer Service: Used solely for initializing the variable type.
Setter Service: Used solely for setting a specific objects properties.

Constructor Service

This service is used for the sole purpose of initializing the Variable Type. The developers should know that this service will be the only one where they should instantiate the given Variable Type. Developers should not instantiate variables outside of this service.

Constructor Variables


Constructor Diagram

The Constructor Server Script is this:

if (!tw.local.encapsulationVar) {
tw.local.encapsulationVar = new tw.object.SbxEncapsulationVar();
}


Setter Service

This service is used for setting the properties of the Variable Type. The developers in your team should use this service for setting properties on the variable type. Clients of this code will pass as input the object that they want to set properties to. And they will also pass as input the variable properties to be set on the given object. The service will return as output the same object but with its properties modified as per the input.

Setter Variables


Setter Diagram

The Setter Server Script has the following code:

tw.local.encapsulationVar.property1 = tw.local.property1;
tw.local.encapsulationVar.property2 = tw.local.property2;

The above script could be made smarter. For example, it could determine if an input property is to be changed by whether it is defined or not.


Client

You application in Teamworks will need to interact with your Variable Type. These services will have business logic and do some processing, the output of that processing will be used for populating your variable type. These services are the clients to your Constructor and Setter services.

Your clients will define a variable of the Variable Type. And they will define other variables that will be used for populating your variable type.

Client Variables


Client Diagram


Wrap up

This article described an approach for how to enforce encapsulation in Teamworks. The design relies on the following:

Variable Type. The constructor and setter services revolve around just one given variable type.
Constructor Service. This service is the only one that should create a given Variable Type.
Setter Service. This service should be the only place in your project where you modify objects of the Variable Type.
Clients. The clients for the encapsulation should abide by the principle of always calling the Constructor and Setter services.

This design principle can not be enforced without the Developers help and knowledge on implementing this approach. The developers in your team should be aware of the pattern and be able to abide by it. Encourage your team to abide by the approach and you will see the benefits.

Among the benefits I have seen are:

* Reduces maintenance cost. Overall, it is an aggregate of the points below.
* Finding the business logic. With this approach it becomes very easy to figure out where your business logic is making changes to your Variable Type.
* Refactoring. If you modify your Variable Type it will become easier to refactor your existing functionality. In this scenario all you need to do is modify the Setter inputs and the server script.


References

http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)

Join The Discussion

Your email address will not be published. Required fields are marked *