Thread: Class Design

  1. #1
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034

    Question Class Design

    Hey guys, ya so I've been thinking about this and coming to a blank. I'd appreciate some advice. I've been using Asio for basic HTTP and SMTP. Just a couple hundred lines of usage code. I've been trying to structure it. If I have classes http_server and smtp_server, whats the most professional way to implement that (ie. not copy and pasting them). First I'm thinking they inherit server_base, but then I'm thinking maybe they are typedefs for basic_server<http::accepter, etc>.

    I did a bit of googling and ran into libnetpp (which wraps Asio). It uses the last method, has an object to the accepter, and relays the requests. That means it has to re-define every method (link is below.. eg. async_connect would exist in the basic_server AND http::accepter). Is that the best way? I was thinking why wouldn't you just have basic_server inherit public http::accepter so you have everything without having to do extra work. Is it because it has no well-defined interface?

    net/client/client.hpp at ab417c151903e2093a0730cd2ea2cea02768caa2 from vinzenz's libnetpp - GitHub

    Are the any patterns or methods associated with solving this problem?

    Below I'll try and show what I'm thinking simplified..

    Code:
    template<typename ACCEPTER>
    class basic_server
    {
    public:
        void accept() { this->accepter.accept(); }
    private:
        ACCEPTER accepter;
    };
    
    namespace http
    {
        class accepter
        {
        public:
            void accept() {}
        }
        
        typedef basic_server<accepter> server;
    }
    
    typedef http::server http_server;
    //same thing with smtp
    That's basic what I see in other people's code.

    Code:
    template<typename ACCEPTER>
    class basic_server
    {
    //dont need anything
    };
    
    namespace http
    {
        class accepter
        {
        public:
            void accept() {}
        }
        
        typedef basic_server<accepter> server;
    }
    
    typedef http::server http_server;
    //same thing with smtp
    That's what I was thinking about doing. Sucks? Both suck? I dunno.

    Sorry I'm kinda tired.. I fell asleep thinking about this. :-/ Thanks!

    Edit: connecter didn't make much sense for a server, so I changed it to accepter

    Also thought this might make more sense (non-virtual interface pattern or CRTP):

    Code:
    class server_base
    {
    public:
        void accept() { asio::accept(); this->onAccept(); }
    protected:
        virtual void onAccept() = 0;
    };
    
    namespace http
    {
        class server : public server_base
        {
        private:
            void onAccept()
            {
                //do something here
            }
        }
    }
    
    typedef http::server http_server;
    //same thing with smtp
    I think Python's Twisted library looks sort of like that. That's more for usage code though.
    Last edited by Dae; 09-13-2009 at 07:59 AM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help on class coupling
    By andrea72 in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2011, 10:16 AM
  2. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  3. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  4. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM