C# 运行时对象创建的设计模式
我有一个类可以帮助发现C# 运行时对象创建的设计模式,c#,design-patterns,runtime,factory-pattern,C#,Design Patterns,Runtime,Factory Pattern,我有一个类可以帮助发现HID设备,当检测到设备时,会引发事件,然后由另一个类检测到,该类将最终负责创建一个对象,以表示HID设备。创建类使用新创建的HID对象引发自己的事件 考虑到这一点,我有几个设计问题: (一) 我对“最佳实践”做了一些研究,涉及在运行时创建未知数量或类型的对象,抽象工厂设计模式在结果中经常出现。抽象工厂设计模式是否适合我的场景,或者我还应该做些什么 (二) HidFinder类引发事件,通知感兴趣的人(主要是HidCreator类)已发现设备。然后,HidCreator类引
HID
设备,当检测到设备时,会引发事件
,然后由另一个类检测到,该类将最终负责创建一个对象
,以表示HID
设备。创建类
使用新创建的HID
对象引发自己的事件
考虑到这一点,我有几个设计问题:
(一)
我对“最佳实践”做了一些研究,涉及在运行时创建未知数量或类型的对象,抽象工厂
设计模式在结果中经常出现。抽象工厂
设计模式是否适合我的场景,或者我还应该做些什么
(二)
HidFinder
类引发事件,通知感兴趣的人(主要是HidCreator
类)已发现设备。然后,HidCreator
类引发一个包含新创建的HID
设备的事件。这似乎是正确的方法,但是,无论采用哪种方法,我们都将不胜感激
下面是有关代码的一个简化示例
public class HidFinder
{
public event EventHandler<HidFoundArgs> HidFoundHandler;
private void DeviceAdded(object sender, EventArrivedEventArgs e)
{
OnHidFoundHandler(new HidFoundArgs());
}
protected virtual void OnHidFoundHandler(HidFoundArgs e)
{
EventHandler<HidFoundArgs> handler = this.HidFoundHandler;
if (handler != null)
{
handler(this, e);
}
}
}
public class HidCreator
{
private readonly HidFinder hidFinder;
public event EventHandler<IHidDevice> HidDeviceCreatedHandler;
public HidCreator(HidFinder hidFinder)
{
this.hidFinder = hidFinder;
this.hidFinder.HidFoundHandler += HidFinderOnHidFoundHandler;
}
private void HidFinderOnHidFoundHandler(object sender, HidFoundArgs hidFoundArgs)
{
// Create a new HID
var newHidDevice = Factory.CreateMethod();
OnHidDeviceCreatedHandler(newHidDevice);
}
protected virtual void OnHidDeviceCreatedHandler(IHidDevice e)
{
EventHandler<IHidDevice> handler = this.HidDeviceCreatedHandler;
if (handler != null)
{
handler(this, e);
}
}
}
公共类HidFinder
{
公共事件事件处理程序HidFoundHandler;
添加了专用void设备(对象发送方,EventArrivedEventArgs e)
{
OnHidFoundHandler(新的HidFoundArgs());
}
受保护的虚拟void OnHidFoundHandler(HidFoundArgs e)
{
EventHandler=this.HidFoundHandler;
if(处理程序!=null)
{
处理者(本,e);
}
}
}
公共类隐藏创建者
{
私有只读HidFinder HidFinder;
公共事件事件处理程序HidDeviceCreatedHandler;
公共HidCreator(HidFinder HidFinder)
{
this.hidFinder=hidFinder;
this.hidFinder.hidfundhandler+=hidfinderonhidfundhandler;
}
私有void HidFinderOnHidFoundHandler(对象发送方,HidFoundArgs HidFoundArgs)
{
//创建一个新的HID
var newHidDevice=Factory.CreateMethod();
OnHidDeviceCreatedHandler(新HidDevice);
}
HidDeviceCreatedHandler上受保护的虚拟无效(IHIDE设备)
{
EventHandler=this.hiddevicreatedHandler;
if(处理程序!=null)
{
处理者(本,e);
}
}
}
它的设计总体上看起来不错,但我要做两个改动:
Factory
看起来像某种全局对象,最好使用依赖项注入,例如更好的单元测试
更改Factory.CreateMethod
以使用参数,因为我们不知道将创建IHidDevice
的具体实现,也不知道是否需要HidFoundArgs
中的其他信息
更改后的代码:
public class HidCreator
{
private readonly HidFinder hidFinder;
private readonly IHidDeviceFactory factory;
public event EventHandler<IHidDevice> HidDeviceCreatedHandler;
public HidCreator(IHidDeviceFactory factory, HidFinder hidFinder)
{
this.factory = factory;
this.hidFinder = hidFinder;
this.hidFinder.HidFoundHandler += HidFinderOnHidFoundHandler;
}
private void HidFinderOnHidFoundHandler(object sender, HidFoundArgs hidFoundArgs)
{
// Create a new HID
var newHidDevice = factory.Create(HidFoundArgs.ToCreationParameters(hidFoundArgs));
OnHidDeviceCreatedHandler(newHidDevice);
}
protected virtual void OnHidDeviceCreatedHandler(IHidDevice e)
{
EventHandler<IHidDevice> handler = this.HidDeviceCreatedHandler;
if (handler != null)
{
handler(this, e);
}
}
}
public interface IHidDeviceFactory
{
IHidDevice Create(HidCreationParameters parameters);
...
}
public class HidCreationParameters
{
...
}
public class HidFoundArgs
{
public static HidCreationParameters ToCreationParameters(HidFoundArgs args)
{
...
}
}
公共类创建器
{
私有只读HidFinder HidFinder;
私有只读IHidDeviceFactory工厂;
公共事件事件处理程序HidDeviceCreatedHandler;
公共HidCreator(IHidDeviceFactory工厂,HidFinder HidFinder)
{
这个工厂=工厂;
this.hidFinder=hidFinder;
this.hidFinder.hidfundhandler+=hidfinderonhidfundhandler;
}
私有void HidFinderOnHidFoundHandler(对象发送方,HidFoundArgs HidFoundArgs)
{
//创建一个新的HID
var newHidDevice=factory.Create(HidFoundArgs.ToCreationParameters(HidFoundArgs));
OnHidDeviceCreatedHandler(新HidDevice);
}
HidDeviceCreatedHandler上受保护的虚拟无效(IHIDE设备)
{
EventHandler=this.hiddevicreatedHandler;
if(处理程序!=null)
{
处理者(本,e);
}
}
}
公共接口IHidDeviceFactory
{
IHidDevice Create(HidCreationParameters);
...
}
公共类HidCreationParameters
{
...
}
公共类HidFoundArgs
{
公共静态HidCreationParameters到CreationParameters(HidFoundArgs args)
{
...
}
}