Why would you create a service class for housing a method like 'battle', rather than just putting it on the ship class itself, so that the code reads more like ship1->battle(ship2):
`class Ship {
[...]
public method battle($opponent)
{
[...]
return $outcome;
}
That's a good question :) In this case the battle logic is so simple than it makes sense to put it on the Ship class but it's not a good design because you are adding more responsibilities to that class. What would happen if in the future the battle process requires other dependencies, like an API client? You won't be able to inject that dependency into the Ship class
In short, it's better to have many small classes than one big and ugly class. Cheers!
Please, log in to vote for this comment
2|
Share Comment
"Houston: no signs of life" Start the conversation!
2 Comments
Why would you create a service class for housing a method like 'battle', rather than just putting it on the ship class itself, so that the code reads more like ship1->battle(ship2):
`class Ship
{
}
$outcome = $ship1->battle($ship2);`
Hey brendanlawton
That's a good question :)
In this case the battle logic is so simple than it makes sense to put it on the Ship class but it's not a good design because you are adding more responsibilities to that class. What would happen if in the future the battle process requires other dependencies, like an API client? You won't be able to inject that dependency into the Ship class
In short, it's better to have many small classes than one big and ugly class. Cheers!
"Houston: no signs of life"
Start the conversation!