简体   繁体   English

是否可以阻止对类的成员变量的某些方法调用

[英]Is it possible to block certain method calls to a member variable of a class

I require to create a Camera class on top of the default camera class for Windows Phone 8, PhotoCaptureDevice 我需要在Windows Phone 8, PhotoCaptureDevice的默认相机类的顶部创建一个Camera类

Ideally I would have wanted to build a child class 理想情况下,我希望建立一个儿童班

class CustomCaptureDevice:PhotoCaptureDevice{
    ....
    ....
}

This would have allowed me to both configure the camera device, and also provide for some additional frame processing technique via CustomCaptureDevice. 这将使我既可以配置摄像头设备,又可以通过CustomCaptureDevice提供一些其他帧处理技术。 Unfortunately, PhotoCaptureDevice is a sealed class , hence I cannot do that. 不幸的是, PhotoCaptureDevice是一个sealed class ,因此我不能这样做。

Now I can think of two alternative methods which could help me tackle the problem: 现在,我可以想到两种可以帮助我解决问题的替代方法:

(1) (1)

class CustomCaptureDevice
{
    private PhotoCaptureDevice captureDevice;

    // Implement all the functions of PhotoCaptureDevice here. And internally redirect them to PhotoCaptureDevice

}

The above method would be too cumbersome. 上面的方法太麻烦了。 And in case of any API changes to PhotoCaptureDevice, could require significant updates to CustomCaptureDevice class. 并且如果对PhotoCaptureDevice进行任何API更改,都可能需要对CustomCaptureDevice类进行重大更新。

(2) (2)

class CustomCaptureDevice
{
    public PhotoCaptureDevice captureDevice;

    // Expose this variable to the user, who can set things in PhotoCaptureDevice as per his needs.

}

The above seems to be an easier solution, since it would require minimal updates in case of API changes to PhotoCaptureDevice. 上面的方法似乎是一个更简单的解决方案,因为在对PhotoCaptureDevice进行API更改的情况下,只需最少的更新即可。

It seems (2) could fit my requirement well. 看来(2)可以很好地满足我的要求。 But I have an additional requirement, because of which (2) could prove unfit. 但是我还有一个额外的要求,因此(2)可能不合适。 I want to disable certain configuration capabilities in PhotoCaptureDevice, since I want to keep a few parameters like exposure and focus not configurable by the user. 我想禁用PhotoCaptureDevice中的某些配置功能,因为我想保留一些用户无法配置的参数,例如曝光和聚焦。

How could I accomplish this in the design framework of (2). 如何在(2)的设计框架中完成此任务。 Is it possible at all? 有可能吗?

I understand that it would be easy to implement by approach (1). 我知道可以通过方法(1)轻松实现。 Please take note of the constraint that PhotoCaptureDevice is a sealed class . 请注意PhotoCaptureDevice密封类的约束。

The first approach is usually called the decorator pattern (ie a wrapper). 第一种方法通常称为装饰器模式 (即包装器)。 I believe that this is exactly the thing you need, as it allows you to provide any functionality for the given interface under the hood. 我相信这正是您需要的东西,因为它允许您为引擎盖下的给定接口提供任何功能。

To make the class interchangeable with a PhotoCaptureDevice in other parts of your app, you will have to implement its relevant interfaces: 为了使该类可与应用程序其他部分中的PhotoCaptureDevice互换,您必须实现其相关接口:

public class ExtendedPhotoCaptureDevice : IDisposable, ICameraCaptureDevice
{
    private readonly ICameraCaptureDevice _actualImplementation;
    ...
}

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

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