如何弃用内部接口

时间:2018-05-09 21:03:19

标签: java deprecated deprecation-warning

我需要弃用Java SDK中的API以使它们更通用。但我无法弄清楚如何做以下情况:

public class AdoptDog {
    public interface OnDogAdoption {
        public void onDogAdoption(String dogName);
    }
    public void adoptDog(final String dogName, OnDogAdoption callbackObj) {
        // Perform asynchronous tasks...
            // Then call the callback:
            callbackObj.onDogAdoption(dogName);
    }
}

SDK的用户拨打电话如下:

AdoptDog adoptDog = new AdoptDog();
adoptDog.adoptDog("Snowball", new OnDogAdoption {
    @Override
    public void onDogAdoption(String dogName) {
        System.out.println("Welcome " + dogName);
    }
};

我想从Dog to Pet推广并弃用提及Dog的API。为了向后兼容,我在弃用API时不必更改上面采用Snowball的代码片段。

我是如何尝试弃用Dog API的:

// Introduce Pet API

public class AdoptPet {
    public interface OnPetAdoption {
        public void onPetAdoption(String petName);
    }
    public void adoptPet(final String petName, OnPetAdoption callbackObj) {
        // Perform asynchronous tasks...
            // Then call the callback:
            if (callbackObj instanceof OnDogAdoption) {
                ((OnDogAdoption) callbackObj).onDogAdoption(petName);
            }
            else {
                callbackObj.onPetAdoption(petName);
            }
    }
}

// Dog API now extends Pet API for backward compatibility

@Deprecated
public class AdoptDog extends AdoptPet {
    @Deprecated
    public interface OnDogAdoption extends AdoptPet.OnPetAdoption {
        @Deprecated
        public void onDogAdoption(String dogName);
    }
    @Deprecated
    public void adoptDog(final String dogName, OnDogAdoption callbackObj) {
        super.adoptPet(dogName, callbackObj);
    }
}

问题是它不完全向后兼容。 SDK的用户必须实现AdoptPet.OnPetAdoption.onPetAdoption(),否则会出现编译错误:

AdoptDog adoptDog = new AdoptDog();
adoptDog.adoptDog("Snowball", new OnDogAdoption {
    @Override
    public void onDogAdoption(String dogName) {
        System.out.println("Welcome " + dogName);
    }

    // PROBLEM: How avoid customers having to implement this dummy method? 
    @Override
    public void onPetAdoption(String petName) {
        assert("This code should not be reached");
    }
};

还有其他方法可以弃用AdoptDog(特别是OnDogAdoption)并保持完全向后兼容吗?

2 个答案:

答案 0 :(得分:5)

Java 8允许您指定default方法实现。您可以使用它来帮助您,例如:

@Deprecated
public interface OnDogAdoption extends AdoptPet.OnPetAdoption {
    @Deprecated
    void onDogAdoption(String dogName);

    default void onPetAdoption(String petName) {
        onDogAdoption(petName);
    }
}

通过使用默认实现,客户端代码将不需要实现它(但如果他们愿意,也可以),因此不应该有编译错误。

注意: All interface methods are public by default - in fact they can only be public - there's no need to specify that.

答案 1 :(得分:3)

您不能延长A signed URL includes additional information, for example, an expiration date and time, that gives you more control over access to your content. This additional information appears in a policy statement, which is based on either a canned policy or a custom policy.OnPetAdoption不是OnDogAdoptionOnPetAdoption也可以处理猫的采用。 OnPetAdoption不能。

我建议你将OnDogAdoption包裹在OnDogAdoption中,并使用该调度程序调用新方法,例如:

OnDogAdoptionDispatcher

这样,您仍然具有向后兼容性和干净的新界面。

如果您使用的是Java 8或更高版本,则不需要单独的课程,您可以这样做

@Deprecated
public class AdoptDog extends AdoptPet {
    @Deprecated
    public interface OnDogAdoption {
        @Deprecated
        public void onDogAdoption(String dogName);
    }

    private static class DogAdoptionDispatcher implements AdoptPet.OnPetAdoption {
            final OnDogAdoption target;
            public DogAdoptionDispatcher(OnDogAdoption target) {
                this.target = target;
            }
            @Override
            public void onPetAdoption(String petName) {
                target.onDogAdoption(petName);
            }
    }

    @Deprecated
    public void adoptDog(final String dogName, OnDogAdoption callbackObj) {
        super.adoptPet(dogName, new DogAdoptionDispatcher(callbackObj));
    }
}