Design issue when having classes implement different interfaces to restrict client actions
- by devoured elysium
Let's say I'm defining a game class that implements two different views:
interface IPlayerView {
void play();
}
interface IDealerView {
void deal();
}
The view that a game sees when playing the game, and a view that the dealer sees when dealing the game (this is, a player can't make dealer actions and a dealer can't make player actions). The game definition is as following:
class Game : IPlayerView, IDealerView {
void play() { ... }
void deal() { ... }
}
Now assume I want to make it possible for the players to play the game, but not to deal it. My original idea was that instead of having
public Game GetGame() { ... }
I'd have something like
public IPlayerView GetGame() { ... }
But after some tests I realized that if I later try this code, it works:
IDealerView dealerView = (IDealerView)GameClass.GetGame();
this works as lets the user act as the dealer.
Am I worrying to much? How do you usually deal with this patterns? I could instead make two different classes, maybe a "main" class, the dealer class, that would act as factory of player classes. That way I could control exactly what I would like to pass on the the public. On the other hand, that turns everything a bit more complex than with this original design.
Thanks