org.perfectjpattern.core.api.extras.delegate
Interface IDelegator<I>

Type Parameters:
I - Type interface that this IDelegator builds.
All Known Implementing Classes:
AbstractDelegator, Delegator, DynamicDelegator

public interface IDelegator<I>

Delegates Design Pattern: Allows multiple objects implementing methods with different names but compatible signatures to be used interchangeably. It will also work with or without a target interface to implement i.e. using IDelegate.

The IDelegator interface simplifies and generalizes the steps in creating an Adapter. Delegates are simple to use, and a single delegate instance may be reused multiple times.

Responsibility: Definition of "Delegator".

Notes: Base source code implemented by Steve Lewis and Wilhelm Fitzpatrick and adapted to fit PerfectJPattern componentization criteria and code conventions.

Example usage:


    //
    // 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");
 

Version:
$Revision: 1.0 $ $Date: Jun 24, 2007 11:58:59 AM $
Author:
Steve Lewis, Wilhelm Fitzpatrick, Giovanni Azua
See Also:
Article: A Java Programmer Looks at C-Sharp Delegates
 

Method Summary
 I build(Class aTarget, String aMethodName)
          Returns Dynamic Proxy that implements the target generic parameter interface.
 I build(Object aTarget, String aMethodName)
          Returns Dynamic Proxy that implements the target generic parameter interface.
 

Method Detail

build

I build(Class aTarget,
        String aMethodName)
        throws IllegalArgumentException,
               UnsupportedOperationException
Returns Dynamic Proxy that implements the target generic parameter interface. The implementing Dynamic Proxy executes the underlying method identified by Class type and Method name.

Parameters:
aTarget - non-null class with a bindable static method.
aMethodName - name of the static method.
Returns:
Dynamic proxy implementing the target generic interface.
Throws:
UnsupportedOperationException - No suitable method found.
IllegalArgumentException - 'aTarget' must not be null.
IllegalArgumentException - 'aMethodName' must not be null.

build

I build(Object aTarget,
        String aMethodName)
        throws IllegalArgumentException,
               UnsupportedOperationException
Returns Dynamic Proxy that implements the target generic parameter interface. The implementing Dynamic Proxy executes the underlying method identified by Object instance and Method name.

Parameters:
aTarget - non-null target with a bindable method.
aMethodName - name of the method.
Returns:
Dynamic proxy implementing the target generic interface.
Throws:
UnsupportedOperationException - No suitable method found.
IllegalArgumentException - 'aTarget' must not be null.
IllegalArgumentException - 'aMethodName' must not be null.


Copyright © 2007-2009. All Rights Reserved.