mercredi 14 juillet 2021

Design Pattern for Mixed Class Relations

I encountered a problem in one of my projects, which I have detailed below. Do you know any design patterns that could solve this problem?

I'm trying to make a game that can be played with 2 people or 1 person vs computer. While the game is being played in PVP mode, I want a third person to be able to join the game in "Spectator" mode only. Also, I want to keep the users who keep the game open but don't take any action as "Idle". I also want to store users as "OnQueue" for online match mode. Players actively playing in a game will also be referred to as "Player".

I thought of a method like this; Let the Player, Spectator, Idle, and OnQueue classes be subclasses of the "Person" abstract class. So I can switch between them using the Factory Design Pattern.

My problem is: The computer can also be a player. But I am not able to include the computer in this "Person Abstract" family. Also, I can't derive Computer from Player, because I need a human for the player as well. I'm a little confused, I hope I was able to explain.

Aucun commentaire:

Enregistrer un commentaire