在我的程序中,我有一个窗口,用户可以在刷信用卡时看到这个窗口。该窗口将等待,直到刷卡或发生任何其他错误。目前,等待窗口正在内部创建读卡器类,并使用读卡器类的委托/回调方法来了解卡上的数据何时可用或发生任何其他错误。
因为有4-5种类型的阅读器可用,所以我正在为CardReader实现策略模式。
我还想将CardReader创建/实例化代码与窗口代码解耦。你能给我一些同样的建议吗?
在为CardReader设计策略模式类之前,我将阅读您的评论。
谢谢
发布于 2011-07-07 20:44:15
使等待窗口构造函数需要引用读取器的类,为OnCardReadEvent注册等待窗口。当接收到事件时,调用读取器类中的函数来检查它,然后如果需要,继续显示等待窗口或关闭它。在窗口关闭时,从OnCardReadEvent注销
如果你没有OnCardReadEvent,那就做一个。
在reader类中,当读取完成时,执行类似以下内容的操作,以使多个窗口能够从单个读取器接收数据
DCardArrived _evnt = OnCardArrived; /*presumably declared event*/
Delegate[] _iList;
DCardArrived _Invoker;
if (_evnt != null)
{
_iList = _evnt.GetInvocationList();
for (int i = 0; i < _iList.Length; i++)
{
//You could also use BeginInvoke
_Invoker = (DCardArrived)_iList[i];
_Invoker.Invoke(this/*Sender*/,CardData/*class that inherits EventArgs containing the data either informing just the window to close or not or with the data for further processing*/);
}
}您没有提供给我们太多的工作:)
https://stackoverflow.com/questions/6609009
复制相似问题