I guess that the most prominent feature of Java 5 is Generics. I must admit that I love them. The type safety you gain outweighs the additional complexity by far. Most examples on the web show you how Generics are used in the context of Collections.
Another ideal application of Generics are policy providers, which store and return implementations of policy interfaces and the lookup of the implementation is done via the interface. Policies are used for customizing application behaviour. They are classes with small complexity and high specialization. A policy could for example be used to determine whether an object in a graphical editor supports drag & drop. Additionally the policy could return the correct command object to perform the necessary changes after a drop event.
The code below uses Generics to define the interface of a policy provider that ensures type safety. Type safety in this case means, that it is not possible to register an implementation for a policy if the implementation does not implement the policy interface.
public interface IPolicyProvider {
/**
* Registers a policy implementation for the given policy type (policy
* interface).
*/
public
/**
* Registers a policy implementation for the given policy type (policy
* interface) and object type / context.
*/
public
/**
* Returns a policy implementation for the given policy type (policy
* interface).
*/
public
/**
* Returns a policy implementation for the given policy type (policy
* interface) and object type / context.
*/
public
}

0 Comments:
Post a Comment
<< Home