org.perfectjpattern.core.api.extras.delegate
Interface IDelegate

All Known Implementing Classes:
AbstractDelegator.DelegateProxy

public interface IDelegate

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 "Delegate" interface. Default target interface that should be used when there is no an explicitly defined target interface.

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: 2.0 $ $Date: Jun 24, 2007 12:28:16 PM $
Author:
Steve Lewis, Wilhelm Fitzpatrick, Giovanni Azua
See Also:
Article: A Java Programmer Looks at C-Sharp Delegates
 

Method Summary
 Object invoke(Object... anArguments)
          Returns the Result of the target call; primitive types are wrapped; may return void result.
 

Method Detail

invoke

Object invoke(Object... anArguments)
Returns the Result of the target call; primitive types are wrapped; may return void result. Executes the target method using the input arguments and returns the appropriate result if any.

Parameters:
anArguments - List of input arguments.
Returns:
Result of the target call; primitive types are wrapped; may return void result.


Copyright © 2007-2009. All Rights Reserved.