OO Design - Object asks a question to a class that indirectly holds it

I am wondering if an object asking a question to another object that indirectly holds it is a “bad” design. For instance...

Requirements: The symbol (object) moves along the grid. When he tries to move to another place, he needs to know if this place is occupied by what blocks him, or if this part of the grid is completely inaccessible. (Note that the character himself must know).

In the application, the state contains the tilemanager and charactermanager. The tile manager knows which tiles are available and which are not. The denominator knows the tile locations of the characters.

It would be reasonable for the character to call a function from a state, say AuthorizeMovement, which determines whether movement through his TileManager and CharacterManager is possible, and returns true if so, false if not?

Is this a violation of any important principles leading to trouble on the road?

Obviously, this is generalized and divided into what is needed to understand the problem.

+5
source share
3 answers

. OO . 4: , , . , . , / .

, , . , , , .

, , .

+1

, , , , . " ", , . :

... , ,

, "" "" , , " ", "" "hold".

.

, - , TileManager / CharacterManager.

+2

- . , State. ?

Abstraction and principles are useful tools. But you should not depend on them to qualify your code as good. Not every abstraction or every principle is good for every scenario or any possible implementation. These are recommendations, not rules. If you cannot quickly see an alternative implementation, use this one and then return to it.

0
source

All Articles