Need a link to the extension interface template in Java

The following Java project allows you to extend an object without changing its interface for clients. An object can implement additional extension interfaces. Clients can request an object for the extension interfaces that it implements. I read about it in a blog post, but my Google skills did not find the blog post again. I am not a supporter of the merits of this design. I just want to find a blog post.

For example, imagine a domain model for vehicles. Each vehicle object implements this interface:

public interface Extendable {

    /**
     * Asks the object if it provides the extension.
     * 
     * @param extensionInterface
     *            requested extension
     * @return object implementing the requested extension, or {@code null} if
     *         not available.
     */
    <T> T queryExtension(Class<T> extensionInterface);
}

Fixed wing aircraft have flight control surfaces, but other types of vehicles do not. Define an interface for the function of control surfaces:

public interface ControlSurfaces {
    String getAilerons();
    String getElevator();
    String getRudder();
}

ControlSurfaces:

public class FixedWingAircraft extends Vehicle {

    @SuppressWarnings("unchecked")
    public <T> T queryExtension(Class<T> extensionInterface) {
        if (ControlSurfaces.class.equals(extensionInterface)) {
            return (T) new ControlSurfacesImpl();
        }
        return null;
    }
}

, , . , .

public class VehicleServiceImpl {

    private VehicleDao vehicleDao;
    private ControlSurfacesDao controlSurfacesDao;

    public void save(Vehicle vehicle) {
        vehicleDao.save(vehicle);

        ControlSurfaces controlSurfaces = vehicle.queryExtension(ControlSurfaces.class);
        if (controlSurfaces != null) {
            controlSurfacesDao.save(vehicle, controlSurfaces);
        }
    }
}
+3
2

Java- " ", " ". 2 (POSA-2), Schmidt, Stal, Rohnert Buschmann, Wiley, 2000.

+4

? , . , .

0

Source: https://habr.com/ru/post/1711453/


All Articles