// Something that "runs" (in some coordinated way) multiple "runnable" things. interface IRunnableOf<T> where : IRunnable // Provide base-class functionality for a "runner" abstract class RunnerBase<T> : IRunnableOf<T> class SequentialRunner<T> : RunnerBase<T> // Same interface,different behavior. class ConcurrentRunner<T> : RunnerBase<T> // other types of runners. class ConcurrentBlockRunner : SequentialRunner<Block> class SequentialBlockRunner : ConcurrentRunner<Block>
现在,如何调和ConcurrentBlockRunner和SequentialBlockRunner?我的意思是:
>通过一个共同的祖先参考,用于收集. (IEnuerable< T,其中T = ??)
>提供额外的基类功能. (例如,添加属性).
我通过添加刚才指定了IA< T>的类型参数的另一个接口来弥补#1:
interface IBlockRunner : IRunnableOf<Block> { }
并将我的ConcurrentBlockRunner和SequentialBlockRunner定义修改为:
class ConcurrentBlockRunner : SequentialRunner<Block>,IBlockRunner class SequentialBlockRunner : ConcurrentRunner<Block>,IBlockRunner
由于ConcurrentBlockRunner和SequentialBlockRunner都使用Block作为其类型参数,这似乎是一个正确的解决方案.不过,我忍不住感到“奇怪”,因为好的,我只是把这个界面打了.
对于#2,我想将几个通用数据添加到ConcurrentBlockRunner和SequentialBlockRunner.有几个属性适用于它们,但不适用于它们唯一的普通基类,它们一直在RunnerBase< T>.
这是第一次使用C#,我感觉到多重继承会有所帮助.如果我能做到:
abstract class BlockRunnerBase { int Prop1 { get; set; } int Prop2 { get; set; } class ConcurrentBlockRunner : SequentialRunner<Block>,BlockRunnerBase class SequentialBlockRunner : ConcurrentRunner<Block>,BlockRunnerBase
然后我可以简单地添加这些额外的属性到BlockRunnerBase,一切都会工作.有没有更好的办法?
我知道我会立即考虑考虑composition,我开始使用:
class BlockRunner : IBlockRunner { IBlockRunner _member; int Prop1 { get; set; } // Wish I could put these in some base class int Prop2 { get; set; } // Lots of proxy calls,and proxy events into _member void Method() { _member.Method(); } event SomeEvent { add { _member.SomeEvent += value; } remove { _member.SomeEvent -= value; } } }
我遇到的问题(驱使我写这个问题)是,一旦你撰写,你就会失去类型的兼容性.在我的情况下,_member正在触发一个事件,所以sender参数的类型是SequentialBlockRunner.但是,事件处理程序正试图将其转换为类型BlockRunner,这当然失败了.解决方案没有使用add / remove来代理事件,而是实际处理它们,并引发我自己的事件.这么多工作只是为了添加几个属性…
解决方法
要更加明确:
class SequentialRunner<T> : RunnerBase<T>
应该实现IRunnableOf< T>并且代理RunnerBase< T>没有继承.
class SequentialRunner<T> : IRunnableOf<T> { private readonly RunnerBase<T> _runnerBase; ... }