如何管理对可交换类的订阅

本文关键字:可交换 何管理 管理 | 更新日期: 2023-09-27 17:58:44

我正试图更好地了解如何维护对一个可能交换的类的订阅(更改策略)。即使这些例子是人为的,我也会尽量保持这一点。

假设有一个类皮肤

public class Skin
{
  //Raised when the form needs to turn on/off a blinking light
  public event BlinkEventHandler BlinkEvent;
  //The back color that forms should use
  public Color BackColor{ get; protected set; }
}

当应用程序启动时,它将读取一个目录,其中包含不同Skin类的配置文件。用户可以随时切换当前皮肤。

我目前的工作使用了一种非常奇怪的策略(IMO),看起来像这样:

/// <summary>
/// Some class that can see when the Skin Changes
/// </summary>
public class SkinManager
{
  //Raised when the Skin changes
  public event SkinChangedEventHandler SkinChangedEvent;
  private static Skin currentSkin;
  public static Skin CurrentSkin {get;}
  public SkinManager(){/* gets a skin into currentSkin */}
  public void ChangeSkin()
  {
    //... do something to change the skin
    if(SkinChangedEvent != null)
    {
      SkinChangedEvent(this, new SkinChangedEventArgs(/*args*/));
    }
  }
}
/// <summary>
/// Some form that follows the Skinning Strategy
/// </summary>
public class SkinnedForm : Form
{
  private Skin skin;
  public SkinnedForm()
  {
    skin = SkinManager.CurrentSkin;
    if(skin != null)
    {
      skin.BlinkEvent += OnBlink;
    }
    SkinManager.SkinChangedEvent += OnSkinChanged;
  }
  private void OnSkinChanged(object sender, SkinChangedEventArgs e)
  {
    //unregister if we have a current skin
    //the local was to ensure that the form unsubscribes
    //when skin changes
    if(skin != null)
    {
       skin.BlinkEvent -= OnBlink;
    }
    skin = SkinManager.CurrentSkin;
    if(skin != null)
    {
       skin.BlinkEvent += OnBlink;
    }
    SkinChanged();
  }
  private void SkinChanged(){ Invalidate(); }
  private void OnBlink(object sender, BlinkEventArgs e)
  {
     //... do something for blinking
  }
}

我不相信这是一个好的实现,相反,我希望看到这样的东西:

/// <summary>
/// Some class that can see when the Skin Changes
/// </summary>
public class SkinManager
{
  //Raised when the Skin changes
  public event SkinChangedEventHandler SkinChangedEvent;
  //Relays the event from Skin
  public event BlinkEventHander BlinkEvent;
  private static Skin currentSkin;
  public static Skin CurrentSkin {get;}
  public SkinManager()
  {
    //... gets a skin into currentSkin
    currentSkin.BlinkEvent += OnBlink;
  }
  /// <summary>
  /// Relays the event from Skin
  /// </summary>
  private void OnBlink(object sender, BlinkEventArgs e)
  {
     if(BlinkEvent != null)
     {
       BlinkEvent(this, e);
     }
  }
  public void ChangeSkin()
  {
    //... do something to change the skin
    if(SkinChangedEvent != null)
    {
      SkinChangedEvent(this, new SkinChangedEventArgs(/*args*/));
    }
  }
}
/// <summary>
/// Some form that follows the Skinning Strategy
/// </summary>
public class SkinnedForm : Form
{
  //Do not need the local anymore
  //private Skin skin;
  public SkinnedForm()
  {
    SkinManager.CurrentSkin.BlinkEvent += OnBlink;
    SkinManager.SkinChangedEvent += OnSkinChanged;
  }
  private void OnSkinChanged(object sender, SkinChangedEventArgs e)
  {
    //Only register with the manager, so no need to deal with
    //subscription maintenance, could just directly to go SkinChanged();
    SkinChanged();
  }
  private void SkinChanged() { Invalidate(); }
  private void OnBlink(object sender, BlinkEventArgs e)
  {
     //... do something for blinking
  }
}

我不确定这是否清楚,但主要有一个局部变量,它被严格用于确保我们在订阅新类上的事件之前取消订阅事件。我认为:我们实现了蒙皮的策略模式(选择你想要使用的蒙皮策略并使用它运行),但每个策略实现都有我们直接订阅的事件。当策略发生变化时,我们希望我们的订阅者观看正确的发布者,以便我们使用本地人。同样,我认为这是一种糟糕的方法。

我建议使用管理器来监视它管理的类的所有事件,并将它们传递出去,这样策略就可以更改,订阅服务器就可以继续侦听正确的事件通知?提供的代码是在我形成问题时创建的,所以请原谅任何错误。

如何管理对可交换类的订阅

通常,当您想为激发事件的类创建代理(包装器)时,您需要取消订阅(分离)前一个实例,与新实例交换,然后订阅(附加)其事件。

假设你的皮肤界面是这样的:

interface ISkin
{
    void RenderButton(IContext ctx);
    event EventHandler Blink;
}

然后你改变它的部分需要看起来像这样:

public void SetSkin(ISkin newSkin)
{
    // detach handlers from previous instance
    DetachHandlers();
    // swap the instance
    _skin = newSkin;
    // attach handlers to the new instance
    AttachHandlers();
}
void DetachHandlers()
{
    if (_skin != null)
       _skin.Blink -= OnBlink;
}
void AttachHandlers()
{
    if (_skin != null)
       _skin.Blink += OnBlink;
}

完整的代理看起来像这样:

interface IChangeableSkin : ISkin
{
    event EventHandler SkinChanged;
}
public class SkinProxy : IChangeableSkin 
{
    private ISkin _skin; // actual underlying skin
    public void SetSkin(ISkin newSkin)
    {
        if (newSkin == null)
           throw new ArgumentNullException("newSkin");
        if (newSkin == _skin)
           return; // nothing changed
        // detach handlers from previous instance
        DetachHandlers();
        // swap the instance
        _skin = newSkin;
        // attach handlers to the new instance
        AttachHandlers();
        // fire the skin changed event
        SkinChanged(this, EventArgs.Empty);
    }
    void DetachHandlers()
    {
        if (_skin != null)
           _skin.BlinkEvent -= OnBlink;
    }
    void AttachHandlers()
    {
        if (_skin != null)
           _skin.BlinkEvent += OnBlink;
    }
    void OnBlink(object sender, EventArgs e)
    {
        // just forward the event
        BlinkEvent(this, e);
    }
    // constructor
    public SkinProxy(ISkin initialSkin)
    {
        SetSkin(initialSkin);
    }

    #region ISkin members
    public void RenderButton(IContext ctx)
    {
        // just calls the underlying implementation
        _skin.RenderButton(ctx);
    }
    // this is fired inside OnBlink
    public event EventHandler BlinkEvent = delegate { }; 
    #endregion

    #region IChangeableSkin members
    public event EventHandler SkinChanged = delegate { }; 
    #region
}

您的表单应该只包含对IChangeableSkin实现的引用。

有点复杂,切换的负担由订户承担。这不太好。

当交换皮肤时,旧皮肤可能会删除其活动订阅者,并可能将其连接到新皮肤。

但一个更整洁的模式可能是一个不会改变并暴露事件的皮肤支架。

SkinnedForm可能具有ISkin-类型的属性

public class SkinnedForm : Form
{
  private ISkin _Skin;
  ...
}

通过公共属性公开它,并在任何时候设置它。通过这种方式,SkinnedForm从不关心ISkin是如何工作的,也不关心它所包含的事件模型。传入新的Skin类引用时,新的OnBlink事件将自动接管。实现ISkin的类应该包含OnBlink的逻辑。

然后,您有一个管理器类(离您指定的不远),它可以获得对新皮肤和相关SkinnedForm的引用。经理的唯一工作就是更新SkinnedForm上的ISkin属性。