Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

template method pattern - naming conventions

I have this abstract class named as RenderableEntity .

I have a public method render() that has some logic wrapped around abstract protected render() method. How should I name this abstract render() method. Is there some kind of convention eg. doRender(), makeRender() for protected method render()?

public abstract class RenderableEntity extends Entity {

    private boolean visible;

    public void render(){
        if(visible){
            render();
        }
    }
    protected abstract void render();
}

Edit: I know this snippet does not compile. I was just wondering how to name this abstract method since I can't have methods with same name and same parameters.

like image 608
jellyfication Avatar asked Jan 28 '13 14:01

jellyfication


2 Answers

First: Only consider special naming for methods like these if they are for exclusive use by the template method. In addition, you should comment these methods stating that they are used by the template method and any modifications should be made with that usage in mind.

The methods that make up replaceable steps in a template method are often called "hook" methods. You'll sometimes see them named with "Hook" at the end.

In your example, you may want to call it renderHook(), though if you can get more specific on the task that it is performing within the template method render() that would be more descriptive.

I have seen doXXX() used, though it's primarily when there is a one-to-one template-to-hook relationship.

A possible suggestion. For a template method stuff():

  • If stuff() is primarily simple control logic around a single hook, name the hook doStuff() (This seems to be the case in your example above)

  • If stuff() orchestrates several hooks, name them independently with Hook suffixes, and do not name any of them the same as the template (in this case, there should be no stuffHook() method.

like image 165
Scott Stanchfield Avatar answered Sep 22 '22 13:09

Scott Stanchfield


There is no convention about it. But I would prefer to see more meaningfull name. E.g. forceRender, renderNow, renderImmideately or something similar which tells the fact it renders somehow differently than just render.

like image 43
kan Avatar answered Sep 24 '22 13:09

kan