是否有一种方法来保护类变量不在函数之外被修改 [英] Is there a way to protect a class variable from being modified outside of a function

查看:168
本文介绍了是否有一种方法来保护类变量不在函数之外被修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个卷变量,我想要保护修改,除非人调用某个函数。有一种方法,使它只能由该函数修改,而不是在类中创建一个私人类。我想创建一个私人类是一个好主意,但我会感兴趣的,如果别人有不同的方法。不应该允许AudioPlayer在不调用SetVolume的情况下更改卷。这是我在这里的代码,但我想知道人们是否有不同的方式。

I have a volume variable that I want to be protected from modification unless the person calls a certain function. Is there a way to make it to where it can only be modified by that function other than creating a private class within the class. I suppose that creating a private class is a good idea, but I would be interested if someone else has a different approach. AudioPlayer should never be allowed to change the volume without calling SetVolume. That's the code I have here, but I wondered if people had a different way.

public class AudioPlayer
{
    private class VolumeManager
    {
        private AudioPlayer mAudioPlayer;
        public VolumeManager(AudioPlayer audioPlayer)
        {
            mAudioPlayer = audioPlayer;
        }

        private float volume;

        public void SetVolume(float _volume)
        {
            volume = _volume;

            //Do other necessary things that must happen when volume is changed
            //This is the point of the question
            mAudioPlayer.ModifyChannelVolume(Volume);
        }
        public float GetVolume()
        {
            return volume;
        }
    }

    private VolumeManager mVolumeManager;
    public AudioPlayer()
    {
        mVolumeManager = new VolumeManager(this);
    }

    public void ModifyVolume(float volume)
    {
        mVolumeManager.SetVolume(volume);
    }
}


推荐答案

问题,就像我看到的,是,即使有一个私人领域,仍然有点直观和自然,想直接分配到字段。我们想非常确定这不会发生。在这种情况下,我建议将它作为一个属性,而不是一个字段,只是纪律,只有分配到属性:

The problem, as I see it, is that even with a private field it's still somewhat intuitive and natural to want to assign directly to the field. We want to make very sure this doesn't happen. In that case, I recommend building this as a property, rather than a field, and just being disciplined about only assigning to the property:

public class AudioPlayer
{

    public float Volume 
    {
       get { return _volume_NeverSetThisDirectly;}
       set 
       {
           _volume = value;
           //Do other necessary things that must happen when volume is changed
           ModifyChannelVolume(_volume_NeverSetThisDirectly);
       }
    }
    [Browsable(false)]
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    [EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
    private float _volume_NeverSetThisDirectly; //Never assign to this directly!
}

这不会强制它达到你要求的程度,但它确实翻转了直觉和自然的方式,有人将在这个类中使用正确的方式,而不是错误的方式的价值。它也是一个很少的代码和复杂性维护的希望。添加属性在很大程度上不会影响在这个类中工作的人,但是,由于我们正在改变使用社会压力,而不是技术禁止,更多的警告信号,我们有,更好。

This won't enforce it to the degree that you're asking for, but it does flip the intuitive and natural way someone will work in this class to use the value in the right way, rather than the wrong way. It's also a heck of a lot less code and complexity to maintain. The addition of the attributes largely won't effect things for someone working in this class already, but since we're changing to use social pressures rather than technical prohibitions, the more warning signs we have up, the better.

当你发现一个奇怪的情况,你想改变音量字段没有所有的其他事情发生,你可以这样做,而不需要对一个私有类实例做奇怪的事情。

This also gives you an opening in the future when you find that one weird case where you do want to change the volume field without all of that other stuff happening, that you can do so without needing to do strange things to a private class instance.

这篇关于是否有一种方法来保护类变量不在函数之外被修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆