JavaFX:公开ObjectProperty成员而不是getter / setter?

时间:2017-10-05 12:35:55

标签: java oop javafx properties encapsulation

我已经研究了som类及其声明,并且我采用了一种新的封装方式,这使得普通的getter / setter方式达到一个实例变量看起来像野蛮...

javafx.scene.Scene内搜索相机,javafx.scene.Camera的对象并查看它的封装。

您也可以在grepcode.com

上看到它

以下是来自JavaFX Scene类:

private ObjectProperty<Camera> camera;

public final void setCamera(Camera value) {
    cameraProperty().set(value);
}

public final Camera getCamera() {
    return camera == null ? null : camera.get();
}

public final ObjectProperty<Camera> cameraProperty() {
    if (camera == null) {
        camera = new ObjectPropertyBase<Camera>() {
            Camera oldCamera = null;

            @Override
            protected void invalidated() {
                Camera _value = get();
                if (_value != null) {
                    if (_value instanceof PerspectiveCamera
                            && !Platform.isSupported(ConditionalFeature.SCENE3D)) {
                        String logname = Scene.class.getName();
                        PlatformLogger.getLogger(logname).warning("System can't support "
                                + "ConditionalFeature.SCENE3D");
                    }
                    // Illegal value if it belongs to other scene or any subscene
                    if ((_value.getScene() != null && _value.getScene() != Scene.this)
                            || _value.getSubScene() != null) {
                        throw new IllegalArgumentException(_value
                                + "is already part of other scene or subscene");
                    }
                    // throws exception if the camera already has a different owner
                    _value.setOwnerScene(Scene.this);
                    _value.setViewWidth(getWidth());
                    _value.setViewHeight(getHeight());
                }
                if (oldCamera != null && oldCamera != _value) {
                    oldCamera.setOwnerScene(null);
                }
                oldCamera = _value;
            }

            @Override
            public Object getBean() {
                return Scene.this;
            }

            @Override
            public String getName() {
                return "camera";
            }
        };
    }
    return camera;
}

Camera getEffectiveCamera() {
    final Camera cam = getCamera();
    if (cam == null
            || (cam instanceof PerspectiveCamera
            && !Platform.isSupported(ConditionalFeature.SCENE3D))) {
        if (defaultCamera == null) {
            defaultCamera = new ParallelCamera();
            defaultCamera.setOwnerScene(this);
            defaultCamera.setViewWidth(getWidth());
            defaultCamera.setViewHeight(getHeight());
        }
        return defaultCamera;
    }

    return cam;
}

我何时应该使用这种方法将实例变量放在javafx.beans.property.ObjectProperty<T>中并添加额外的抽象层,它将如何或以何种方式有用?

注意:我知道向属性添加侦听器的可能性 - 但还有其他原因吗?

1 个答案:

答案 0 :(得分:1)

你在这里看到的是一种延迟初始化的实现。

属性camera在初始化之前不会被初始化。

否&#34;额外的抽象层&#34;在代码段中显示:cameraProperty()公开camera内部属性(并在第一次调用时初始化它)。访问器和mutator方法是cameraProperty().get()cameraProperty().set(...)的简单快捷方式。

在这个答案中解释了同样的事情: Exposing properties for binding