P
- the type of the product to contain.@Immutable public abstract class LocatableContainer<P> extends LocatableProvider<P> implements Container<P>
P
.
In the following example the type parameter is specified as
String
:
package com.company.spec;
import net.java.truecommons.services.LocatableContainer;
public abstract class StringContainer
extends LocatableContainer<String> {
}
An implementation could now implement this service as follows:
package com.company.impl;
import com.company.spec.StringContainer;
public class GreetingContainer extends StringContainer {
\@Override
public String get() {
// Return the same instance on each call!
return "Hello Christian!";
}
}
Next, the implementation needs to advertise its service by providing a file
with the name META-INF/services/com.company.spec.StringContainer
on the run time class path with the following single line content:
com.company.impl.GreetingContainer
If multiple container services are locatable on the class path at run time, the service with the greatest priority gets selected.
Finally, a client could now simply compose a container according to the
StringContainer
specification by calling:
package com.company.client;
import net.java.truecommons.services.Locator;
import com.company.spec.StringContainer;
public class Main {
public static void main(String[] args) {
Locator l = new Locator(Main.class); // specify calling class
Container<String> c = l.container(StringContainer.class);
String s = c.get(); // obtain product
System.out.println(s); // use product
}
}
Note that multiple calls to c.get()
would always return the same
product again because c
is a container, not a factory.
Implementations should be thread-safe.
Locator
Constructor and Description |
---|
LocatableContainer() |
Copyright © 2012–2016 Schlichtherle IT Services. All rights reserved.