Example of the Block idiom.


interface Command
{
    void execute();
}

class Client
{
    public void register(Service service, final ArgType arg)
    {
        final LocalVarType local;
        ...
        service.setAction(
            new Command()
            {
                public void execute()
                {
                    arg.method();
                    local.method();
                    field.method();
                    method();
                }
            });
        ...
    }
    private void method() ...
    private FieldType field;
}

class Service
{
    public void setAction(Command newAction)
    {
        action = newAction;
    }
    public void onEvent()
    {
        ... action.execute(); ...
    }
    private Command action;
}

About the Author

Kevlin Henney is a Principal Technologist with QA Training in the UK.