Quick tour

Decouplink is an implementation of dynamic links for Java. It is designed to help you assemble software systems from parts that are open for extension, but closed to modification.

Most software systems can never be completely closed to modification. When there is no longer a need for new requirements, it is usually because the software is no longer in use.

Is it unrealistic then, to make a software system closed to modification? In a sense, yes it is. However, the good news is that software systems are made from smaller parts - e.g. components, modules, libraries, classes, etc. If we can keep those smaller parts closed to modification, then our software becomes much easier to understand, maintain and evolve.

Ideally, new requirements should be implemented by adding new parts to a system without modifying existing ones. When this is possible, we say that a system is open for extension.

Creating a system that is open for extension requires good design skills. However, having the right tools matters too. This is where dynamic links come into play:

The essense of dynamic links is very simple: A dynamic link can connect objects of unrelated types. While being simple, the idea is powerful, as it allows developers to extend software systems in ways that would otherwise not be possible without imposing modification.

Let us look into an example. One of the most useful types in the Java library is the String class. Imagine that we want String's to contain information about the Language they are written in - this is not supported by the Java library. It turns out this new feature is really tricky to introduce:

  • String is final and thus we cannot subclass it to add new methods - i.e. we cannot add new fields and methods such as setLanguage() and getLanguage().
  • Even if we could subclass String, in a realistically sized system, we would have to go through a lot of code to make it use our new version. Many bits and pieces of our system would not be closed to modification.

Dynamic links enter the scene. With a dynamic link we can connect objects even when their types are unrelated. Why is this useful? Instead of modifying an existing type, e.g. String, to support a new kind of information, we merely attach that new information, e.g. the String's Language, using a dynamic link. Check it out in the example below:

package com.decouplink.demo.hello;

import static com.decouplink.Utilities.*;

public class Main {

    public static enum Language { ENGLISH, GERMAN, SPANISH, DANISH };

    public static void main(String[] args) {
        // Create a string and have it printed.
        // Note that this method knows nothing
        // about the Language type.
        String s = createEnglishString();
        printStringAndLanguage(s);
    }

    public static String createEnglishString() {
        // Create a string and specify its
        // language using a dynamic link.
        String s = "Hello world";
        context(s).add(Language.class, Language.ENGLISH);
        return s;
    }

    public static void printStringAndLanguage(String s) {
        // Print the string and its language.
        Language lang = context(s).one(Language.class);
        System.out.println(s + " (language: " + lang + ")");
    }
}

The important thing to note is that a String object and a Language object gets connected even though the two types are unrelated. Anyone with access to a String object and knowledge of the Language type can check to see if the String has a Language. Note that the main() method uses a normal String and knows nothing about the Language type.

The magic happens via the special context() method. This is a static method that can be imported and thus used anywhere. Behind the scenes is a small runtime system that keeps track of how objects are connected.

This is, in a nutshell, what dynamic links can do for you. As you start to use dynamic links, you discover that they affect your software designs. We have used dynamic links in a medium-sized component-based system and find the potential stimulating. However, we could use your experience too. Get your copy of Decouplink today, and start using it right away. Let us know how you like it.

Finally, we are hoping to publish a peer-reviewed paper on Decouplink later this year. It will contain much more motivation, detail, and experience. We will link to it from this website when it becomes available. Stay tuned!