P
- type of the public part of the objectR
- type of the restricted part of the objectpublic final class Restrictable<P,R> extends Object
This class is used to assist a pattern that gives the creator of an object more rights.
For example, assume we have a service that offers some business logic, but the service can also be started and stopped for example). We don't want to expose start() and stop() to all consumers of the service. Those methods should be visible (or allowed) just for the component in charge of managing the services (or the creator of the service).
Restrictable
along with a factory method could be used to restrict
access to privileged functionality.
The usage of this class has some advantages. Avoids mixing the API for two groups of people: privileged and regular users. Privileged users use a different API to perform restricted operations. It also avoids a state-based API where the same sequence of method calls might work or might throw an exception, depending on whether the consumer is a privileged user or a regular one.
Example:
public interface PublicService { public void consumeService(); } public interface RestrictedService { public void star(); public void stop(); } // Note: Configuration is not relevant for this example, however it also provides // a way of configuring the object by the creator (Give more rights to the creator to // configure the object). Once the object is configured it cannot be changed. public class Configuration { // This configuration is specific for the implementation. // Different implementations may be configured differently. // Make setters public. // Make getters package private if possible. } public final class ServiceImplementation implements PublicService { private Configuration configuration; // Must be final since it will be accessible by the creator and the reference cannot longer be retrieved (refreshed) after creation. private final RestrictedService restrictedApi; private ServiceImplementation(Configuration configuration) { if (configuration == null) { throw new NullPointerException("configuration cannot be null"); } this.configuration = configuration; this.restrictedApi = new RestrictedApiImpl(); } public static Restrictable<MyPublicService, RestrictedService> create(Configuration configuration) { ServiceImplementation restrictable = new ServiceImplementation(configuration); return Restrictable.create((MyPublicService)restrictable, restrictable.restrictedApi); } @Override public void consumeService() { // ... } private void doSart(){ // ... } private void doStop(){ // ... } private class RestrictedApiImpl implements RestrictedService { // This class is non-static so it uses the same state of ServiceImplementation. // It just implements the restricted part. // This implementation could just call private methods in ServiceImplementation. @Override public void start() { doSart(); } @Override public void stop() { doStop(); } } }
public interface RestrictedApi { public void restrictedMethod(); } public final class MyRestrictable { // Must be final since it will be accessible by the creator and the reference cannot longer be retrieved (refreshed) after creation. private final RestrictedApi restrictedApi = new RestrictedApi() { @Override public void restrictedMethod() { doRestrictedMethod(); } }; private MyRestrictable() { } public static Restrictable<MyRestrictable, RestrictedApi> create() { MyRestrictable restrictable = new MyRestrictable(); return Restrictable.create(restrictable, restrictable.restrictedApi); } private void doRestrictedMethod(){ // ... } // ... }
Modifier and Type | Method and Description |
---|---|
static <P,R> Restrictable<P,R> |
create(P publicPart,
R restrictedPart)
Creates a restrict-able object.
|
P |
getPublic()
Gets the public part of the object.
|
R |
getRestricted()
Gets the restricted part of the object.
|
public static <P,R> Restrictable<P,R> create(P publicPart, R restrictedPart)
publicPart
- object's public partrestrictedPart
- objects's restricted partpublic P getPublic()
public R getRestricted()
Copyright © 2015. All Rights Reserved.