dimanche 13 décembre 2020

Can I create my own methods in a subclass using the template method design pattern?

Whether using methods created outside the abstract class will disturb the Template Method design pattern? Will it still be Template Method pattern if I create MyOwnMethod and call it inside a method?

public abstract class TemplateMethodClass {

    public final void TemplateMethod() {
      a();
      b();
      c();
    }

    protected abstract void a();

    protected abstract void b();

    protected abstract void c();
}
public class SubClass extends TemplateMethodClass {

    public void MyOwnMethod() {
        System.out.println("I am not from template");
    }

    @Override
    protected void a() {
        MyOwnMethod();
    }

    @Override
    protected void b() {}

    

    @Override
    protected void c() {}
}

Aucun commentaire:

Enregistrer un commentaire