Let's say I want to create a simple tic-tac-toe game and i have model for the gameboard. And my question is, where should i put logic for managing this board. For example.
export class Board{
boardString: string;
rows: string[];
separator: string;
constructor(boardString: string, separator: string = '|'){
this.boardString = boardString;
this.separator = separator;
this.rows = this.boardString.split('|');
}
}
// inside model class
atIndex(index: number): string{
const atCol = index % this.rows.length;
const atRow = (index - atCol) / this.rows.length;
return this.rows[atRow][atCol];
}
isFinished(): boolean {
// check if game is finished
}
getRow(index): string{
return this.rows[index]
}
vs
// or in the Service
atIndex(index: number): string{
const atCol = index % this.boardModel.rows.length;
const atRow = (index - atCol) / this.boardModel.rows.length;
return this.boardModel.rows[atRow][atCol];
}
isFinished(): boolean{
// check if game is finished
}
getBoardRow(index): string{
return this.boardModel.rows[index];
}
I don't know if I made myself clear. Tic-tac-toe it's just an example. I want to know where should I put logic responsible for managing model properties? I read from documentation that all logic should be inside services. But for me in some cases it fits more to the model class (e.g., getters). Is it an anti-pattern to keep methods inside model class?
- inside model class (Board)
- inside service that uses this model (GameService)
- combine above two
- create services especially for that model (BoardService) with all needed functions and inject it into another service (GameService)