tags:

views:

143

answers:

2

I'm writing a Login page in wicket by java and want to write it as general as possible so I need to pass to class a function which famous as Function Pointer in C++. The class is:

class LoginForm extends Form
{

    public LoginForm(String id,TextField username,TextField password,WebResponse webResponse)
    {
        super(id);
    }

    @Override
    public void onSubmit()
    {
        String password = Login.this.getPassword();
        String userId = Login.this.getUserId();
        String role = authenticate(userId, password);
        if (role != null)
        {
            if (Login.this.getSave())
            {
                utilities.CreateCookie("Username", userId, false, 1209600, (WebResponse) getRequestCycle().getResponse());
                utilities.CreateCookie("Password", password, false, 1209600, (WebResponse) getRequestCycle().getResponse());
            }
            User loggedInUser = new User(userId, role);
            WiaSession session = (WiaSession) getSession();
            session.setUser(loggedInUser);
            if (!continueToOriginalDestination())
            {
                setResponsePage(UserHome.class);
            }
        }
        else
        {
            wrongUserPass.setVisible(true);
        }
    }
}

where authenticate is that function what should I do?

+2  A: 

You can use inner class

SjB
inner class is cleaning the question not solving it!!!!
JGC
+7  A: 

Just pass an Interface which defines the authenticate method.

void DoSomething(IAuthenticationProvider authProvider) {
    // ...
    authProvider.authenticate();
}
Henrik
Yes, in Java you use interfaces instead of function pointers. So define an interface which defines the function you want to call and then call the interface as in example above. To implement the interface you can use anonymous inner class if you don't want an overhead of additional file.
Gregory Mostizky