gpt4 book ai didi

oop - 管理对象间关系

转载 作者:行者123 更新时间:2023-12-02 06:45:37 24 4
gpt4 key购买 nike

如何编写对象的特殊情况?

例如,假设我正在编写一个角色扮演游戏 - 有 N = 5 个类。矩阵中有 N^2 个关系,可以确定角色 A 是否可以攻击(或对角色 B 使用能力 M)(暂时忽略其他因素)。

我如何在 OOP 中编写代码而不到处放置特殊情况?

换句话说,我可能有一些东西

ClassA CharacterA;
ClassB CharacterB;

if ( CharacterA can do things to CharacterB )

我不确定 if 语句中的内容,而是它是

if ( CharacterA.CanDo( CharacterB ) )

或者一个元类

if ( Board.CanDo( CharacterA, CharacterB ) )

CanDo 函数何时应该依赖于 ClassA 和 ClassB,或者属性/修饰符与 ClassA/ClassB?

最佳答案

我将从 canSee(Monster monster) 或 canBeSeenBy(Monster monster) 方法开始,看看会发生什么。你可能会得到一个可见性类或最终使用 http://en.wikipedia.org/wiki/Visitor_pattern .一个极端的例子是 uncle bobs triple dispatch:

// visitor with triple dispatch. from a post to comp.object by robert martin http://www.oma.com
/*
In this case, we are actually using a triple dispatch, because we have two
types to resolve. The first dispatch is the virtual Collides function which
resolves the type of the object upon which Collides is called. The second
dispatch is the virtual Accept function which resolves the type of the
object passed into Collides. Now that we know the type of both objects, we
can call the appropriate global function to calculate the collision. This
is done by the third and final dispatch to the Visit function.
*/
interface AbstractShape
{
boolean Collides(final AbstractShape shape);
void Accept(ShapeVisitor visitor);
}
interface ShapeVisitor
{
abstract public void Visit(Rectangle rectangle);
abstract public void Visit(Triangle triangle);
}
class Rectangle implements AbstractShape
{
public boolean Collides(final AbstractShape shape)
{
RectangleVisitor visitor=new RectangleVisitor(this);
shape.Accept(visitor);
return visitor.result();
}
public void Accept(ShapeVisitor visitor)
{ visitor.Visit(this); } // visit Rectangle
}
class Triangle implements AbstractShape
{
public boolean Collides(final AbstractShape shape)
{
TriangleVisitor visitor=new TriangleVisitor(this);
shape.Accept(visitor);
return visitor.result();
}
public void Accept(ShapeVisitor visitor)
{ visitor.Visit(this); } // visit Triangle
}

class collision
{ // first dispatch
static boolean Collides(final Triangle t,final Triangle t2) { return true; }
static boolean Collides(final Rectangle r,final Triangle t) { return true; }
static boolean Collides(final Rectangle r,final Rectangle r2) { return true; }
}
// visitors.
class TriangleVisitor implements ShapeVisitor
{
TriangleVisitor(final Triangle triangle)
{ this.triangle=triangle; }
public void Visit(Rectangle rectangle)
{ result=collision.Collides(rectangle,triangle); }
public void Visit(Triangle triangle)
{ result=collision.Collides(triangle,this.triangle); }
boolean result() {return result; }
private boolean result=false;
private final Triangle triangle;
}
class RectangleVisitor implements ShapeVisitor
{
RectangleVisitor(final Rectangle rectangle)
{ this.rectangle=rectangle; }
public void Visit(Rectangle rectangle)
{ result=collision.Collides(rectangle,this.rectangle); }
public void Visit(Triangle triangle)
{ result=collision.Collides(rectangle,triangle); }
boolean result() {return result; }
private boolean result=false;
private final Rectangle rectangle;
}
public class MartinsVisitor
{
public static void main (String[] args)
{
Rectangle rectangle=new Rectangle();
ShapeVisitor visitor=new RectangleVisitor(rectangle);
AbstractShape shape=new Triangle();
shape.Accept(visitor);
}
}

关于oop - 管理对象间关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/948564/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com