Context

From cdd
Jump to: navigation, search

The site dictionary.com defines context as "the set of circumstances or facts that surround a particular event, situation, etc.". The snippet "a particular event, situation, etc" is very close to the definition of behaviour in CDD which is something that operates in a context and encloses mutable or immutable state.


CDD-context.png


What distinguishes static context from dynamic context is that static context is actively selected by the developer when the code is written (compile time) while dynamic context is selected based on a context variable that is only known at runtime.

Static context

If we look at the account example, it has behaviour representations for the two contexts source and destination:

SourceAccount sourceAccount = createAccount().balance(1000).asSourceAccount();
DestinationAccount destinationAccount = createAccount().balance(100).asDestinationAccount();


public class SourceAccount extends AccountMutableBehaviour {
    ...
    public void transferTo(DestinationAccount destination, double amount) { ... }
    ...
}


public class DestinationAccount extends AccountValue {
    ...
    public void deposit(double amount) { ... }
    ...
}


The two representations contains the relevant operations to be used in each context.


Dynamic context

A dynamic context can vary at runtime within a static context. If we take a look at the dynamic context code example:

Person person = createPerson.name("Anders").phoneNumber("011-11111111").asPerson(userRole);

...the context variable userRole is used by the factory to choose a suitable representation which enables us to have different behaviour implementations for different contexts within the static context.


< Back          Next >