简体   繁体   English

Android多态性:反模式?

[英]Android Polymorphism: Anti-Pattern?

I'm reading O'Reilly's "Programming Android" book, and I'm trying to wrap my head around the "Overrides and Callbacks" section starting on page 99. They use this as an example of good code: 我正在阅读O'Reilly的“ Programming Android”(编程Android)一书,并试图从第99页开始围绕“ Overrides and Callbacks”(重写和回调)部分。他们使用它作为良好代码的示例:

public class MyModel {
    public MyModel(TextView textBox) {
        textBox.addTextChangedListener(
            new TextWatcher() {
                public void afterTextChanged(Editable s) {
                    handleTextChange(s);
                }
                // ...
    }
    void handleTextChange(Editable s) {
        // do something with s, the changed text.
    }
}

And later call this an anti-pattern due to lack of extensibility encapsulation: 由于缺乏 可扩展性 封装,后来将其称为反模式:

public class MyModel implements TextWatcher {
    public MyModel(TextView textBox) {
        textBox.addTextChangedListener(this);
    }

    public void afterTextChanged(Editable s) {
        handleTextChange(s);
    }

    // ...

    void handleTextChange(Editable s) {
        // do something with s, the changed text.
    }
}

I don't see the functional difference between the two, aside from the second being a lot more readable. 除了第二个更具可读性之外,我看不到两者之间的功能差异。 Both take a TextView, and implement a handler function to override. 两者都采用TextView,并实现了处理程序函数来覆盖。 Wouldn't the second be just as easy to extend with something like this? 用这样的东西扩展第二个内容不是很容易吗?

public class AnotherModel extends MyModel {
    @Override
    void handleTextChange(Editable s) {
         // another implementation
    }
}

an anti-pattern due to lack of extensibility 由于缺乏可扩展性而导致的反模式

Extensibility-wise, they're similar in that both approaches let a subclass easily modify the existing TextChangeListener by overriding handleTextChange ; 在扩展性方面,它们是相似的,因为这两种方法都可以让子类通过覆盖handleTextChange轻松地修改现有的TextChangeListener but they're different in that only approach #2 also makes it easy for a subclass to add a new TextChangeListener without modifying the existing (inherited) one. 但是它们的不同之处在于,唯一的方法#2还使子类可以轻松添加新的 TextChangeListener而不修改现有(继承)的TextChangeListener

Even if the superclass uses approach #1, the subclass could still add a new TextChangeListener by using approach #2; 即使超类使用方法1,子类仍然可以使用方法2添加新的TextChangeListener but if we're talking about the pattern to use in general, then a consistent use of approach #2 will afford more extensibility than a consistent use of approach #1. 但是,如果我们要讨论的是一般使用的模式,那么与方法1的一致使用相比,方法2的一致使用将提供更多的可扩展性。

I wouldn't like the 2nd form, since the class implements an interface as a trick, not because the class naturally is a subtype of the interface. 我不喜欢第二种形式,因为该类将接口实现为一种技巧,而不是因为该类自然是该接口的子类型。

The 2nd form can be manageable for simple cases, so it's ok unless it gets too messy. 第二种形式在简单情况下是可以管理的,因此除非杂乱无章,否则还可以。

In Java 8, we can use the 1st form in a better syntax: 在Java 8中,我们可以以更好的语法使用第一种形式:

    textBox.addTextChangedListener(this#handleTextChange);  // method reference

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM