0

I am trying to learn the best practices for methods in OOP in general. So lets say I have this code (C# by the way):

class Player
{
    private Connection conn;
    public Update(Keyboard key)
    {
        if(key.IsPressedKey("a"))
        {
            // should move
            conn.SendPacketMovement(Movement.Left);
        }
    }
}

If I wanted to call a method inside the Connection class instance, what should I do? I dont want to add a return value to the Update method because it doesn't seem like a really good practice.

What should I do in that case, when I want several classes to do different things? Should I use delegates and events? If so, how do I subscribe to them. Should I subscribe to them when I create the instance of Player?

4

1 回答 1

1

最后,一个方法总是在另一个方法的范围内被调用。然而,委托确实是一个很好的做法。例如,如果您的类行为可能由不同的委托以不同方式实现,您可以应用责任链模式来减少耦合http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern

于 2012-08-20T05:34:56.547 回答