Main Page

From cdd
Revision as of 06:43, 11 September 2013 by Tengstrand (Talk | contribs) (typo)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


Context Driven Development (CDD) is a new approach to object orientation, which allows for a clean separation of state and behaviour; enabling us to switch an object's context without breaking its encapsulation. The secret sauce that makes this possible is code generation. Having all of the CDD foundation classes generated automatically frees developers to just focus on what the system does instead of how it does it - resulting in less code, cleaner code and more robust code.

Why CDD?

There are a number of issues with "standard" object orientation like Java or C#:

  • The absence of the concept of context which makes it difficult to use objects in different contexts and which leads to non-object-oriented solutions that force you to break encapsulation and violate the Tell Don't Ask principle.
  • There is no correlation between an objects attributes/properties and how the state is built (constructors) and how identity is implemented (hashCode/equals).
  • Poor flexibility at the object level when it comes to composing objects, switching context and code reuse.

CDD tries to break down classes/objects into smaller pieces to tackle these problems. Let's explain the basic concepts by looking at this Java class:

public class Person {
    private int age;
    private String name;
    public Person(int age, String name) {
        this.age = age; = name;
    public boolean isTeenager() {
        return age >= 13 && age <= 19;
    // To be implemented!
    public int hashCode() { ... }
    public boolean equals(Object o) { ... }
    public String toString() { ... }

An instance person can now be created by calling the constructor of the class:

Person person = new Person(25, "James");

What happens here is that we merge data (age, name) with the function/method isTeenager to form an object, which is called encapsulation:


If we look at the Java code again, there is a lot of things going on there:

  1. The class is responsible for building the state (the constructor).
  2. Identity is managed by the class/object (hashCode / equals).
  3. The toString method is managed by the class/object.

By intertwining all these activities we get a rigid and inflexible system. CDD solves the problem by breaking down classes/objects into three pieces/roles, creator, state and behaviour:


With CDD (by defining a PersonState, PersonCreator and Person) we can now create an instance like this:

Person person = createPerson().age(37).name("James").asPerson();

By breaking down classes/objects into smaller pieces we get a more flexible system where every piece is more focused on its task!

/Joakim Tengstrand

Next >