Delegates Pattern
Allows multiple objects implementing methods with different names but compatible signatures to be used interchangeably.
Java does not support Delegates natively therefore the need for this pattern, see A
Java Programmer Looks at C# Delegates article. See also Sun's critic of Delegates.
The introduction of the Delegates Pattern in PerfectJPattern is more a means to componentize other Design Patterns
rather than offering Delegates as an ultimate design tool for building applications. The Delegates feature is
nevertheless included in PerfectJPattern public API for cases where would be needed e.g. it effectively abstracts
from low-level Java Reflection machinery.
Componentized Delegates Pattern
UML Class Design
Snippet
//
// Example class that defines two methods that match in signature
//
class Example
{
//--------------------------------------------------------------------
public static void
staticMethod(String aValue)
{
System.out.println(aValue);
}
//--------------------------------------------------------------------
public void
memberMethod(String aValue)
{
System.out.println(aValue);
}
}
//
// Defines a common interface with method printValue
//
interface IPrinter
{
//--------------------------------------------------------------------
public void
printValue(String aValue);
}
//
// Create instance of example class
//
Example myInstance = new Example();
//
// Example using DynamicDelegator without well defined target interface
//
IDelegator<IDelegate> myDynamicDelegator = new DynamicDelegator(
Void.TYPE, String.class);
//
// Build appropriate IDelegate references
//
IDelegate myDelegate1 = myDynamicDelegator.build(Example.class,
"staticMethod");
IDelegate myDelegate2 = myDynamicDelegator.build(myInstance,
"memberMethod");
//
// Use the Delegate references to invoke the target methods.
//
myDelegate1.invoke("Value1");
myDelegate2.invoke("Value2");
//
// Example using well defined target interface
//
IDelegator<IPrinter> myPrinterDelegator = new Delegator(
IPrinter.class);
//
// Build appropriate IPrinter references
//
IPrinter myPrinter1 = myPrinterDelegator.build(Example.class,
"staticMethod");
IPrinter myPrinter2 = myPrinterDelegator.build(myInstance,
"memberMethod");
//
// Use the IPrinter interface reference.
//
myPrinter1.printValue("Value1");
myPrinter2.printValue("Value2");
|