Skip to content

Instantly share code, notes, and snippets.

@danking
Created February 17, 2015 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danking/f0cfb4bd1a45973022fc to your computer and use it in GitHub Desktop.
Save danking/f0cfb4bd1a45973022fc to your computer and use it in GitHub Desktop.
Inability to connect behavior with a Class

Inability to connect behavior with a Class

Use two interfaces: A and AFactory

A is the interface.

AFactory implements all the factory methods as well as behavior relevant to the Class rather than any particular object.

Then you have

public class MyA implements A {

    // ...

    public class MyAFactory implements AFactory<MyA> {
        // ...
    }

}

If something else is parameterized by the interface C, we can do:

public class Container<T extends A> {

    private final AFactory<T> factory;

    private Container(AFactory<T> factory) {
        this.factory = factory;
    }

    // ...
}

Now wherever you’d like to write

T.foo(t1, t2)

you can write

factory.foo(t1, t2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment