Chapter 6. Patterns and Anti-Patterns

Table of Contents

Anti-Patterns
PicoContainer usage
Use Interfaces
Splitting Work
...

There are various patterns we want to be used and there have been various anti-patterns we don't want to see again. This section provides an illustration of what is good and what should be avoided and give reasons about it.

Under Construction

Not completed yet.

Anti-Patterns

PicoContainer usage

The PicoContainer will resolve dependencies between objects. For this to work properly one needs to have all dependencies as parameters of the constructors. One common misuse is to get a dependency with an indirection.

public class SkypeManager {
    private final SarosNet sarosNet;
    private final IPreferenceStore preferences;


    public SkypeManager(Saros saros) {
        this.sarosNet = saros.getSarosNet();
        this.preferences = saros.getPreferenceStore();
    }
}

The above code has dependencies on SarosNet and IPreferenceStore and they are resolved through the Saros instance. This is a problem when it comes to testing, there might not be a mock for the SarosNet or the IPreferenceStore store and one might have an NullPointerException. The below code shows how to properly express the dependencies.

public class SkypeManager {
    private final SarosNet sarosNet;
    private final IPreferenceStore preferences;


    public SkypeManager(SarosNet sarosNet, IPreferenceStore preferences) {
        this.sarosNet = sarosNet;
        this.preferences = preferences;
    }
}

Use Interfaces

TODO: E.g. ISarosSession vs. Session, talk about testability

Splitting Work

Describe.. splitting work across different threads and synch. issues

...

MORE