java - 这是一种反模式还是违反了一些设计原则?

标签 java design-patterns coding-style anti-patterns design-principles

我自己尝试设计模式和原则并提出了一个问题。 之前,为糟糕的编码风格习惯感到抱歉!!

在这种情况下,我有一个类似 ITest 的接口(interface):

public interface ITest
{
    public void method1();
}

然后将方法和字段(如果有的话)实现到具体类 B 中,如下所示:

public class B implements ITest
{
    //This is the method from the interface
    @Override
    public void method1()
    {
        System.out.println("method1");
    }

    //This is another method in class B
    public void method2()
    {
        System.out.println("method2");
    }
}

现在在应用程序代码中我是这样写的:

public class Main 
{
    public static void main(final String args[]) throws Exception 
    {
        //One principle says:
        //programm to an interface instead to an implementation
        ITest test = new B();

        //method from interface
        test.method1();
        //this method is not accessible because not part of ITest
        test.method2();     //compile-time error
    }
}

您看到类 B 中的方法 2() 不可用,因为对于 ITest 的接口(interface)。 现在,如果我需要这个“重要”方法怎么办? 有几种可能性。我可以在接口(interface)中对其进行抽象,或者使 B 类抽象并扩展到另一个类等,或者在 main() 方法中进行引用,例如:

B test = new B();

但是这样就违反了原则。 因此,我将界面修改为:

public interface ITest
{
    //A method to return the class-type B
    public B hook();
    public void method1();
}

并将实现放在类 B 中:

public class B implements ITest
{
    //this returns the object reference of itself
    @Override
    public B hook()
    {
        return this;
    }

    //This is the method from the interface
    @Override
    public void method1()
    {
        System.out.println("method1");
    }

    //This is the 'important' method in class B
    public void method2()
    {
        System.out.println("method2");
    }
}

现在,在我的 main() 方法中,我可以使用一个小钩子(Hook)或链接机制调用这两个方法,而无需引用新对象,也不会违反设计原则,而且我不需要额外的类来进行扩展或抽象。

public class Main
{
    public static void main(final String args[])
    {
        //programm to an interface instead into an implemintation
        ITest test = new B();
        //method from interface
        test.method1();
        //method2 will not be accessible from ITest so we referencing B through a method hook()
        //benefits: we don't need to create extra objects nor additional classes but only referencing
        test.hook().method2();
        System.out.println("Are they both equal: "+test.equals(test.hook()));
    }
}

此外,我还可以封装、继承和抽象其他方法、字段等。 这意味着,我可以创建更复杂、更灵活的层次结构。

我现在的问题: 这是一种反模式、糟糕的设计原则,还是我们可以从中受益?

感谢观看。 :-)

最佳答案

Is this a kind of anti-pattern, bad design-principle or could we benefit from this?

是的,这是一个糟糕的模式。

问题源于您已将 ITestB 紧密耦合。假设我想创建一个新的 ITest 实现 - 我们称它为 C

public class C implements ITest
{
    @Override
    public B hook()
    {
        // How do I implement this?
    }

    @Override
    public void method1()
    {
        System.out.println("method1");
    }
}

我们无法实现此方法。唯一合理的做法是返回 null。这样做会迫使我们界面的任何用户不断执行防御性空检查。

如果他们每次都必须在使用该方法的结果之前进行检查,那么他们还不如执行一个instanceof 并转换为B。那么你要增加什么值(value)?您只是让界面变得不那么连贯且更加困惑。

关于java - 这是一种反模式还是违反了一些设计原则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50253508/

相关文章:

java - 关闭 Struts 2 中的 OGNL 警告

oop - 四种设计模式的组合编程语言是否独立?

Git:在提交/推送之前通过过滤器运行?

c++ - 避免在默认模板中使用尖括号

java - 阐明报告 - 错误 'assemble' 目标需要 'war' 打包

java - 逆向 : get audited entities referencing to entities that are not audited

java - 如何在括号内使用 , 运算符拆分字符串

architecture - 考虑到数据库中的实际数据,我将如何在 RDBMS 和 DocDBMS 之间进行选择?

c# - 抽象工厂设计模式

coding-style - 你如何记录你的编码标准?