P
- the type of the products to decorate.@Immutable public abstract class LocatableDecorator<P> extends LocatableFunction<P> implements Decorator<P>
P
.
The following example accompanies the example for LocatableContainer
,
so the type parameter is specified as String
again:
package com.company.spec;
import net.java.truecommons.services.LocatableDecorator;
public abstract class StringDecorator
extends LocatableDecorator<StringBuilder> {
}
An implementation could now implement this service as follows:
package com.company.impl;
import com.company.spec.StringDecorator;
public class SmalltalkDecorator extends StringDecorator {
\@Override
public String apply(String s) {
// decorate the given string with a new string and return it!
return s + " 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.StringDecorator
on the run time class path with the following single line content:
com.company.impl.SmalltalkDecorator
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 container with some decorators
according to the StringContainer
and
StringDecorator
specification by calling:
package com.company.client;
import net.java.truecommons.services.Locator;
import com.company.spec.StringContainer;
import com.company.spec.StringDecorator;
public class Main {
public static void main(String[] args) {
Locator l = new Locator(Main.class); // specify calling class
Container<String> c = l.factory(StringContainer.class,
StringDecorator.class);
String s = c.apply(); // obtain product
System.out.println(s); // use product
}
}
Note that multiple calls to c.apply()
would always return the same
product again because c
is a container, not a factory.
Implementations should be thread-safe.
Locator
Constructor and Description |
---|
LocatableDecorator() |
Copyright © 2012–2016 Schlichtherle IT Services. All rights reserved.