Search code examples
c#membersderived-instances

Handling Specified Member Classes In C#


In building a class structure, I would like to have derived classes potentially posses derived member classes. For example:

class GamePiece
{ 
}
class Checker : GamePiece
{}
class ChessMan : GamePiece
{}

class Game 
{
   protected GamePiece _piece;
}

class Checkers : Game
{
   Checkers(){ _piece = new Checker(); }
}
class Chess : Game
{
   Chess(){_piece = new ChessMan();}
}

This is seriously oversimplified, but makes the actual point. Now assuming that there are events and such, I would like to attach the common ones in the base class constructor, and the specialized ones in the derived constructor. For example both a checker and a chessman might have a "captured" event and a "moved" event. I would like to attach them in the base constructor. However, events that would be specific such as "castled" or something like that would be attached only in the specific constructor.

The problem I have is that the base constructor seems to only be able to run BEFORE the derived constructor. How do I effect this such that I get to actually instantiate the "gamepiece" before I call the base "Game" constructor to attach to events.

My gut suggests that this is better handled by removing that functionality from the constructor and simply having an "Attach()" member function and handling it there. However, I want to make sure I am going in the right direction, since it would seem there should be a way to do it in the constructor.


Solution

  • You could try the Template design pattern:

    class Game
    {
        Game() { Init(); }
        protected virtual void Init() {}
    }
    

    Now you can insert generic event handling in the Game Init method and override it with concrete handling logic in the descendants.