How to group generic classes?

I am working on a universal game engine for simple board games, etc. I define the interfaces that will be required for each game. I have classes like IGame, IGameState, IBoardEvaluator and IMove.

I have methods like IGame.PerformMove (IMove move) that I would like to limit. If I play tic-tac-toe, I would like me to use only the specific classes TTTGame, TTTState, TTTMove, etc.

I can come up with several ways to do this, but none of them sound funny. Perhaps all classes can have one common parameter, and I could make sure that it matches.

so IGame<T> has method PerformMove(IMove<T> move) 

If this works, I don’t know which class to use for T. Maybe it doesn’t matter.

My other idea is putting a bunch of general parameters on IGame and providing all the classes I need. Therefore i would createclass TTTGame<TTTMove,TTTState,TTTMove....>

This is also not very. Is there a general outline for this?

+3
source share
3 answers

I don’t see what advantage you get from indicating that your TTTGame class can only accept TTTMoves.

I think you may be too complicated here.

The only thing you protect yourself from is some kind of MonopolyMove scammer who becomes self-aware and puts himself in your code.

I say a stick with interface definitions and avoid generics unless you have a really valid case. I do not see what you were talking about.

+2
source

- . , , , .

,

TTTGame : IGame 
{
    PerformMove(TTTMove move);
}

, , .

public interface IGame<T> where T:IMove
    {
        void PerformMove(T move);
    }

public class TTTGame : IGame<TTTMove>
    {

        public void PerformMove(TTTMove move)
        {
            //perform move
        }

    }

IGame IMove, , ... , (IState..etc), .

+1

, , , , .

IGame ? , tic-tac-toe Quake. , (, ), ?

+1

Source: https://habr.com/ru/post/1725803/


All Articles