I am developing a chess program in Java and consider the following problem:
- Part of the rook requires execution to move in straight lines.
- The bishop element requires execution to move along diagonal lines.
but
- The Queen part requires implementation for both of the above motion patterns.
I canβt come up with a clean solution to model these relationships, I looked at some, but none of them match good object-oriented design and efficient code.
Java does not support multiple inheritance, so Queen cannot borrow an implementation from Rook and Bishop
If Rook and Bishop expanded the Queen, I would need to extract the logic for each type of movement into separate methods, this would seriously inflate my current design for how the movement is tested.
None of the above solutions look elegant enough to just beat:
- By introducing the entire implementation of the motion into the parent class of all parts, in this way, all of them can share the entire common implementation (which is quite a lot)
I know that solution 3 violates a good Java design, but in this case, this design pattern, apparently, only leads to bloated, inelegant solutions.
This can probably be avoided with a complete restructuring of the program, but until now, everything looks pretty effective for me, does OO design always depend on the directness of functionality and structure? Is my approach wrong for language style?
How would you solve this problem?
java oop chess
David wood
source share