What is a hook method?

Asked

Viewed 2,039 times

5

I am studying some design standards and came across this method, however, its concept was not clear to me.
What the method does and how it relates to the Template Method?
I’d like an example in java. Part I didn’t understand:

hook methods are methods that allow extension. The superclass has a public primary method that is invoked by its customers. This method delegates part of its execution to the hook method, which is an abstract method that must be implemented by the subclass...

  • 1

    You could put an excerpt from the material that quotes this?

  • @Pablo Ai is the passage.

  • Got it. I’m writing the answer.

2 answers

7


These methods hook cited are used in the Template pattern. Just as the excerpt you quoted mentions, there is an abstract class A that delegates parts of its functionality to abstract methods. A concrete class B that inherits from this class need to implement these methods so that the code Compile. See an example:

public abstract class MinhaClasseAbstrata {
    public abstract int porcaoPersonalizada(String parametro);

    public int acaoPrincipal(String parametro) {
        return parametro.length() + porcaoPersonalizada(parametro);
    }
}

public MinhaClasseConcreta extends MinhaClasseAbstrata {

    public int porcaoPersonalizada(String parametro) {
        return parametro.length() + 55; // um cálculo qualquer específico para esse caso
    }

}


public MinhaOutraClasseConcreta extends MinhaClasseAbstrata {

    public int porcaoPersonalizada(String parametro) {
        return parametro.length() + 23; // um cálculo qualquer específico para esse caso
    }

}

That way, you know you can call acaoPersonalizada in any copy of the class MinhaClasseAbstrata that there will be a guaranteed return, based on the definition of porcaoPersonalizada of the specific type of the object in question.

  • Thanks for the attention, man! But I could explain this part: "which delegates parts of its functionality to abstract methods..." What it means to delegate a part?

  • 1

    It’s kind of a beautiful expression for "calling another method to do the job". :)

  • @Could you make a small analogy in your reply about the Template method and hook method?

  • Could you make an analogy between the hook method and the Template method in your reply? Thank you!!

  • They are not comparable things. One thing is part of the other. Hook is the name by which we call these methods which are called by the abstract class.

  • 1

    Now I realized that I had written in a confused way. Reread the first sentence I edited and see if it’s clear.

Show 1 more comment

5

The "Hook Method" acts as a placeholder within "Template Method".

This means that it is a method declared in the base class, but is only defined in the derived classes.

Take a closer look at this link, which explains about the Template Method Pattern: https://sourcemaking.com/design_patterns/template_method

Browser other questions tagged

You are not signed in. Login or sign up in order to post.