Inner Class inside an Interface implementing same Interface, what we are achieving by this?

Hisham Muneer :

My Question:

I was looking at the source code of TextWatcher and I didn't get the concept here. What was the point of extending to NoCopySpan?

TextWatcher.java:

public interface TextWatcher extends NoCopySpan {
     public void beforeTextChanged(CharSequence s, int start, int count, int after);
     public void onTextChanged(CharSequence s, int start, int before, int count);
     public void afterTextChanged(Editable s);
}

NoCopySpan.java:

package android.text;

/**
 * This interface should be added to a span object that should not be copied into a new Spanned when performing a slice or copy operation on the original Spanned it was placed in.
 */
public interface NoCopySpan {
    /**
     * Convenience equivalent for when you would just want a new Object() for
     * a span but want it to be no-copy.  Use this instead.
     */
    public class Concrete implements NoCopySpan {}
}
vsminkov :

NoCopySpan is just a marker interface. According to javadoc it is used to modify behavior of copy routine of Spanned objects (it relies on type of components). For example android.text.SpannableStringBuilder uses such inheritance information to skip spans copying upon construction.

This approach has some drawbacks but still pretty common. The reason of Concrete class existence is to provide convenient way to construct on-op dummy (or default) realization of NoCopySpan interface.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=427198&siteId=1