处理不同类型对象之间交互的设计模式
Design pattern to handle the interaction between different types of objects
我有一个 C# 程序,它有一个名为 Ball
的 class,它有两个名为 BallColor
和 BallType
的 enum
字段。有 7 种球颜色(红色、蓝色、黄色等)和 7 种球类型(网球、足球、保龄球等)。球对象可以具有任何可能的颜色和类型组合。
在我的程序中,我有许多 Ball
个不同颜色和类型组合的对象。球可以通过碰撞相互影响。当两个球发生碰撞时,它会触发一个由名为 handleInteraction(ball1, ball2)
的函数处理的交互。两个球之间的相互作用取决于每个球的类型和颜色。例如,如果任何保龄球击中任何乒乓球,乒乓球就会被摧毁,保龄球会继续以原来的速度运动。但是,如果任何足球与保龄球相撞,足球会从保龄球上弹开,保龄球的速度会降低n
。另一个例子,如果一个红色的网球与一个绿色的乒乓球相撞,乒乓球从网球上弹开,乒乓球的颜色变成与网球相同的颜色(红色)。
在我的 handleInteraction(ball1, ball2)
函数中,我一直在使用嵌套的 switch
语句处理交互。但是,随着我编写更多代码来处理所有情况,嵌套 switch
语句似乎不是正确的方法。
谁能想出更好的方法来处理球之间的相互作用?
一种解决方案是为交互结果和球创建接口:
public interface IInteractionResult
{
void Handle();
}
public interface IBall
{
BallTypeEnum BallType { get; }
IInteractionResult HandleInteraction(IBall ball);
}
并实现每个可能的 classes 实现 IInteractionResult
其中交互逻辑在 Handle
方法中提供:
public class DestroyInteractionResult : IInteractionResult
{
public void Handle()
{
//your code for destroy result behaviour
}
}
public class BounceInteractionResult : IInteractionResult
{
public void Handle()
{
//your code for bounce result behaviour
}
}
public class NothingInteractionResult : IInteractionResult
{
public void Handle()
{
//your code for nothing
}
}
在此之后为 Ball
实现你的 classes,它将在 HandleInteraction
的实现中用 switch
语句实现 IBall
。 SoccerBall
例如:
public class SoccerBall : IBall
{
public BallTypeEnum BallType
{
get { return BallTypeEnum.Soccer; }
}
public IInteractionResult HandleInteraction(IBall ball)
{
switch (ball.BallType)
{
case BallTypeEnum.Soccer:
return new BounceInteractionResult();
case BallTypeEnum.Bowl:
return new DestroyInteractionResult();
case BallTypeEnum.PingPong:
return new BounceInteractionResult();
// and so on
}
throw new NotImplementedException("Undefined ball type");
}
}
为每种球类型和每次交互分离 classes 有助于您将每种类型的单个逻辑收集到单个 class。
而你 handleInteraction
看起来像:
public void handleInteraction(IBall ball1, IBall ball2)
{
var interaction = ball1.HandleInteraction(ball2);
interaction.Handle();
}
我认为这是最灵活的解决方案。某种策略模式。
更好的方法是在球 Class 中多放一些 属性,如下所示。更像是实际物理
prop double Mass { get; set;)
handleInteraction 内部
If(b1.Mass / b2.Mass < 0.1) // Make your Own ratios
//Destrioy Ball
else if(..)
//Move in derecton or Bounce
我的方法是,为所有行为制作属性
考虑抽象化碰撞的概念。有一个关于速度、颜色等的规则。将这些规则放在 table 中,然后查找并应用它们。
For example, if any bowling ball hits any ping pong ball, the ping pong is destroyed and the bowling ball continues to move with its original velocity.
球在碰撞后可以被摧毁(或不被摧毁)。 (布尔值)
However, if any soccer ball collides with a bowling ball, the soccer ball bounces off the bowling ball and the bowling ball's velocity is reduced by n.
一个球的速度减少了n。
球可以改变方向 ("bounces off of")。
Another example, if a red tennis ball collides with a green ping pong ball, the ping pong ball bounces off the tennis ball and the color of the ping pong ball is changed to the same color of the tennis ball (red).
球可以改变颜色。
在这些示例中,有 4 个维度的规则。如果您非常确定该模型,您可以轻松地编写一些代码来定义每次碰撞的规则(在 table 或地图中)并在没有很多条件的情况下应用规则。
我有一个 C# 程序,它有一个名为 Ball
的 class,它有两个名为 BallColor
和 BallType
的 enum
字段。有 7 种球颜色(红色、蓝色、黄色等)和 7 种球类型(网球、足球、保龄球等)。球对象可以具有任何可能的颜色和类型组合。
在我的程序中,我有许多 Ball
个不同颜色和类型组合的对象。球可以通过碰撞相互影响。当两个球发生碰撞时,它会触发一个由名为 handleInteraction(ball1, ball2)
的函数处理的交互。两个球之间的相互作用取决于每个球的类型和颜色。例如,如果任何保龄球击中任何乒乓球,乒乓球就会被摧毁,保龄球会继续以原来的速度运动。但是,如果任何足球与保龄球相撞,足球会从保龄球上弹开,保龄球的速度会降低n
。另一个例子,如果一个红色的网球与一个绿色的乒乓球相撞,乒乓球从网球上弹开,乒乓球的颜色变成与网球相同的颜色(红色)。
在我的 handleInteraction(ball1, ball2)
函数中,我一直在使用嵌套的 switch
语句处理交互。但是,随着我编写更多代码来处理所有情况,嵌套 switch
语句似乎不是正确的方法。
谁能想出更好的方法来处理球之间的相互作用?
一种解决方案是为交互结果和球创建接口:
public interface IInteractionResult
{
void Handle();
}
public interface IBall
{
BallTypeEnum BallType { get; }
IInteractionResult HandleInteraction(IBall ball);
}
并实现每个可能的 classes 实现 IInteractionResult
其中交互逻辑在 Handle
方法中提供:
public class DestroyInteractionResult : IInteractionResult
{
public void Handle()
{
//your code for destroy result behaviour
}
}
public class BounceInteractionResult : IInteractionResult
{
public void Handle()
{
//your code for bounce result behaviour
}
}
public class NothingInteractionResult : IInteractionResult
{
public void Handle()
{
//your code for nothing
}
}
在此之后为 Ball
实现你的 classes,它将在 HandleInteraction
的实现中用 switch
语句实现 IBall
。 SoccerBall
例如:
public class SoccerBall : IBall
{
public BallTypeEnum BallType
{
get { return BallTypeEnum.Soccer; }
}
public IInteractionResult HandleInteraction(IBall ball)
{
switch (ball.BallType)
{
case BallTypeEnum.Soccer:
return new BounceInteractionResult();
case BallTypeEnum.Bowl:
return new DestroyInteractionResult();
case BallTypeEnum.PingPong:
return new BounceInteractionResult();
// and so on
}
throw new NotImplementedException("Undefined ball type");
}
}
为每种球类型和每次交互分离 classes 有助于您将每种类型的单个逻辑收集到单个 class。
而你 handleInteraction
看起来像:
public void handleInteraction(IBall ball1, IBall ball2)
{
var interaction = ball1.HandleInteraction(ball2);
interaction.Handle();
}
我认为这是最灵活的解决方案。某种策略模式。
更好的方法是在球 Class 中多放一些 属性,如下所示。更像是实际物理
prop double Mass { get; set;)
handleInteraction 内部
If(b1.Mass / b2.Mass < 0.1) // Make your Own ratios
//Destrioy Ball
else if(..)
//Move in derecton or Bounce
我的方法是,为所有行为制作属性
考虑抽象化碰撞的概念。有一个关于速度、颜色等的规则。将这些规则放在 table 中,然后查找并应用它们。
For example, if any bowling ball hits any ping pong ball, the ping pong is destroyed and the bowling ball continues to move with its original velocity.
球在碰撞后可以被摧毁(或不被摧毁)。 (布尔值)
However, if any soccer ball collides with a bowling ball, the soccer ball bounces off the bowling ball and the bowling ball's velocity is reduced by n.
一个球的速度减少了n。
球可以改变方向 ("bounces off of")。
Another example, if a red tennis ball collides with a green ping pong ball, the ping pong ball bounces off the tennis ball and the color of the ping pong ball is changed to the same color of the tennis ball (red).
球可以改变颜色。
在这些示例中,有 4 个维度的规则。如果您非常确定该模型,您可以轻松地编写一些代码来定义每次碰撞的规则(在 table 或地图中)并在没有很多条件的情况下应用规则。