我试图获得更好的感觉,以便如何维护可能交换的类的订阅(更改策略)。即使这些例子是人为设计的,我也会尽量保持这一点。如何管理对可交换类的订阅
假设有一类皮肤
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; }
}
应用程序启动时,它会读取的目录全配置文件用于不同的皮肤类。用户可以随时切换当前皮肤。
我目前的工作使用了一个非常奇怪的策略(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
}
}
我不确定这是否清楚,但主要是有一个局部变量,严格使用,以确保我们在订阅新类上的事件之前取消订阅事件。我认为它是这样的:我们实施了蒙皮策略模式(选择您想要使用的蒙皮策略并运行它),但是每个策略实施都有我们直接订阅的事件。当战略发生变化时,我们希望我们的订阅者观看正确的发布者,以便我们使用当地人。再次,我认为这是一种可怕的方法。
是否有一个我为使用管理器来监控所管理的类的所有事件并将它们传递的策略而改变并且订户继续侦听正确事件通知的转换的名称?所提供的代码是在我形成问题时即时创建的,因此可以原谅任何错误。
太棒了,谢谢。这正是我所提议的,但要确保我没有脱离基地。那么这就是所谓的代理呢?出于某种原因,他们在这里称之为提供者,但是当我查找模式时,它似乎不匹配。 – MPavlak 2011-06-08 14:51:49