依赖注入接线问题
如果有如下 3 个接口
public interface IWeapon {
void Kill();
}
public interface ISword:IWeapon {
void Slice();
}
public interface IShuriken: IWeapon {
void Pierce();
}
public class Ninja {
public IWeapon Weapon {get;set;}
public void BrutalKill() {
/* warrior must pierce, pierce, pierce and then kill
*/
}
public void HonorKill {
/* warrior must kill by one slice */
}
}
对于这样的场景,您将如何连接容器以及 BrutalKill 和 HonorKill 的方法体是什么样的?
编辑:根据评论,我在想忍者应该配备武器......如果它想配备剑或手里剑......应该稍后决定......不确定我是否在想是的..也许我们需要将 Ninja 子类为 NinjaWithShuriken 和 NinjaWithSword
If there are 3 interfaces like the following
public interface IWeapon {
void Kill();
}
public interface ISword:IWeapon {
void Slice();
}
public interface IShuriken: IWeapon {
void Pierce();
}
public class Ninja {
public IWeapon Weapon {get;set;}
public void BrutalKill() {
/* warrior must pierce, pierce, pierce and then kill
*/
}
public void HonorKill {
/* warrior must kill by one slice */
}
}
For a scenario like this how would you wireup the container and what would your method body look like for BrutalKill and HonorKill ?
EDIT: Based on comments, I was thinking on the lines a ninja should be armed with a weapon... if it wants to be armed with a sword or shuriken...should be decided later... not sure if i am thinking right .. maybe we need to subclass Ninja as NinjaWithShuriken and NinjaWithSword
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不确定这是否是您要找的,但我想要这个:
更新
我喜欢 Phil Sandler 的评论,因此快速更新以反映这一点:
更新
基于对原始问题的更新,我想说:
我们的想法是,我们并不真正关心剑和手里剑如何实施攻击,只要忍者在需要时可以使用它们来履行他的职责。暗杀可以按照特定忍者的意愿进行,只要工作在规定的协议范围内完成,在这种情况下是通过攻击。
Not sure if this is what you are looking for, but I would have this:
Update
I like Phil Sandler's comment so a quick update to reflect that:
Update
Based on the update to the original question I would say:
The idea being that we don't really care how Sword and Shuriken implement Attack, as long as the ninja can use them to perform his duty when called upon. The assassination can be caried out how the specific ninject wishes, as long as the job gets done within the confines of the stated agreement, in this case by Attacking.
如果你的忍者能够进行残酷杀戮和荣耀杀戮,他绝对必须拥有ISword和ISShuriken。 Ninja 依赖于这些,所以我们在 ctor 中声明它们:
这是我们的武器:
让我们获得这些依赖项的几个实现:
我们的配置如下所示:
我们的入口点如下所示:
If your ninja is able to BrutalKill and HonorableKill, he absolutely must have a ISword and a IShuriken. Ninja is dependent on these, so we declare them in the ctor:
Here's our weapons:
Let's get a couple implementations of these dependencies:
Our configuration looks like this:
And our entry point looks like this:
Kill()
,没有参数,看起来像是在命令忍者自杀。我将定义Ninja
来作用于其他忍者:然后,提高从武器到杀戮动作的抽象级别:
并让忍者支持杀戮类型:
现在我们可以担心武器:
和杀戮动作:
这里是一个示例接线(根据需要转换为您的 IoC 容器):
Kill()
, with no parameters, seems like you are commanding the ninja to commit suicide. I would defineNinja
to act on other ninjas:Then, raise the level of abstraction from weapon to kill move:
and have ninjas support the kill types:
Now we can worry about weapons:
and kill moves:
Here is a sample wiring (translate to your IoC container as necessary):
统一:
假设忍者同时拥有剑和手里剑,因为只有剑可以切片,只有手里剑可以刺穿。
此外,Ninja 有一个接受 IShuriken 和 ISword 作为参数的构造函数。
所以为了获得忍者,
In unity:
Assuming that Ninja has both Sword and Shuriken since only Sword can slice and only Shuriken can pierce.
Also, Ninja has a constructor that accepts IShuriken and ISword as arguments.
And so to get Ninja,