mercredi 26 avril 2017

Design pattern for requiring children of a base class to call a method from the base class

Let's say I have a base class

public class BaseAuthenticator
{
    protected void AuthenticateSuccessfully()
    {
        // Do stuff
    }
}

And I have many types of authenticators all deriving from the base class. I only want one thing from the derived classes : It's that they don't forget to call AuthenticateSuccessfully() somewhere in their code. All authenticators have their own logic, but in the end they should always call AuthenticateSuccessfully() somewhere when ready to do so (after validations, etc.).

How can I ensure at compile time that every derived authenticator calls the AuthenticateSuccessfully() method at least once?

Aucun commentaire:

Enregistrer un commentaire