Java:为每个扩展类创建一个方法摘要

Mar*_*aux 2 java design-patterns keyword

这样做有任何关键字或设计模式吗?

请检查更新

public abstract class Root
{
    public abstract void foo();
}

public abstract class SubClass extends Root
{
    public void foo()
    {
        // Do something
        //---------------- Update -------------------//
        // This method contains important code
        // that is needed when I'm using a instance
        // of SubClass and it is no instance of any
        // other class extending SubClass

    }
}

public class SubberClass extends SubClass
{
    // Here is it not necessary to override foo()
    // So is there a way to make this necessary? 
    // A way to obligate the developer make again the override 
}
Run Code Online (Sandbox Code Playgroud)

谢谢

Mic*_*yan 9

如果你这样做,那么你可能会滥用继承; 与流行的神话相反,继承不是用于制作自定义钩子/处理程序,而是用于实现替代实现.

如果您希望用户提供某种函数/钩子/回调,那么您应该定义一个接口,该接口仅提供您需要用户定义的那些方法.然后,您应该要求用户将该接口的实例传递给对象的构造函数或传递给需要它的函数.

聚合,委托和组合通常比继承更好,更安全的设计模式; 强迫其他用户从您的类继承,这是非常危险的,因为它为用户提供了许多违反类的合同或使基类的不变量无效的机会.