6

I'm building a large Desktop application, which will have a range of functionality, for each I wish to have a specific class.

I'm thinking of using a nested class structure to modularize the code and ensure separation of interface vs implementation:

class Manager
{
  public:
    Manager(); //Initialise *t and *wapi in constructor
    ~Manager(); //Delete *t and *wapi in destructor
  private:
    class Transport;
    Transport *t;
    class WinAPI;
    WinAPI *wapi;
};

class Manager::Transport
{
  public:
    Transport(); //Initialise *tcp and *http in constructor
    ~Transport(); //Delete *tcp and *http in destructor
  private:
    class TCPSocket;
    TCPSocket *tcp;
    class HTTP;
    HTTP *http;
};

class Manager::Transport::TCPSocket {};
class Manager::Transport::HTTP {};

class Manager::WinAPI 
{ 
  ... 
};

This is the first time I've built a large embedded application - are there any issues/other considerations I should consider before taking this approach?

  • 4
    As a (sort of) aside, a class named just "Manager" makes me immediately wonder whether this might not be some close relative of a God Class. – Jerry Coffin Mar 8 '17 at 17:56
  • 1
    You'll want to define or delete the copy-constructor and/or assignment operator (and the move versions of those) to avoid certain bugs. Look up the Rule of Three / Rule of Five. – Sjoerd Mar 9 '17 at 15:07
7

Immediate questions are:

  1. Are TCP sockets and HTTP connections really specific to your Manager::Transport nested class?
  2. How will you unit test them if they're not accessible outside Manager?
  3. Will anything other than a Manager ever need a Transport?

to which at a guess I'd probably answer No, With unnecessary difficulty, and Maybe, respectively.

You don't need this nesting to modularize the code - if anything it seems to have encouraged moving unrelated declarations into the same place.

Socket wrapper, and logical connection objects, look like part of a platform abstraction layer. This would normally go below your business object layer, not inside it.

  • 2
    "Below" and "Inside" are basically the same thing, from the perspective of abstraction. I think the question is more about encapsulation. If the author can do the necessary testing and preserve encapsulation, that is the best. – Frank Hileman Mar 8 '17 at 17:57
  • 2
    Even if the fact that your Manager uses Sockets is an implementation detail worth encapsulating, Sockets are a well-understood abstraction independent of what you do with them. I'd normally use nested classes when factoring out implementation details that don't make any sense outside the original class, and could never be used in isolation. – Useless Mar 9 '17 at 9:39
  • @Useless thanks for this answer and comments - very helpful. – Babra Cunningham Mar 9 '17 at 12:35
  • 1
    @Useless bad username! Your comment is very useful! – Frank Hileman Mar 9 '17 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.