P
- the type of the products to modify.@Immutable public abstract class LocatableModifier<P> extends LocatableFunction<P> implements Modifier<P>
P
.
The following example accompanies the example for LocatableFactory
,
so the type parameter is specified as StringBuilder
again:
package com.company.spec;
import net.java.truecommons.services.LocatableModifier;
public abstract class StringBuilderModifier
extends LocatableModifier<StringBuilder> {
}
An implementation could now implement this service as follows:
package com.company.impl;
import com.company.spec.StringBuilderModifier;
public class SmalltalkModifier extends StringBuilderModifier {
\@Override
public StringBuilder get(StringBuilder b) {
// Modify and return the same instance on each call!
return b.append(" How do you do?");
}
}
Next, the implementation needs to advertise its service by providing a file
with the name META-INF/services/com.company.spec.StringBuilderModifier
on the run time class path with the following single line content:
com.company.impl.SmalltalkModifier
If multiple modifier services are locatable on the class path at run time, they are applied in ascending order of their priority so that the result of the modifier service with the greatest number becomes the result of the entire modifier chain.
Finally, a client could now simply compose a factory with some modifiers
according to the StringBuilderFactory
and
StringBuilderModifier
specification by calling:
package com.company.client;
import net.java.truecommons.services.Locator;
import com.company.spec.StringBuilderFactory;
import com.company.spec.StringBuilderModifier;
public class Main {
public static void main(String[] args) {
Locator l = new Locator(Main.class); // specify calling class
Factory<StringBuilder> f = l.factory(StringBuilderFactory.class,
StringBuilderModifier.class);
StringBuilder b = f.get(); // create product
System.out.println(b.toString()); // use product
}
}
Note that multiple calls to f.get()
would always return a new
product because f
is a factory, not a container.
Implementations should be thread-safe.
Locator
Constructor and Description |
---|
LocatableModifier() |
Copyright © 2012–2016 Schlichtherle IT Services. All rights reserved.