Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pros & cons of a callback (std::function/std::bind) vs an interface (abstract class)

I'm creating a server application in C++11 using Boost.Asio. I've created a class, Server, which takes care of accepting new connections. It's basically just:

void Server::Accept() {   socket_.reset(new boost::asio::ip::tcp::socket(*io_service_));   acceptor_.async_accept(*socket_,                          boost::bind(&Server::HandleAccept, this, boost::asio::placeholders::error)); }  void Server::HandleAccept(const boost::system::error_code& error) {   if (!error) {     // TODO   } else {     TRACE_ERROR("Server::HandleAccept: Error!");   }   Accept(); } 

I've found two ways (I'm sure there are more) to "fix" the TODO comment, i.e. to move the socket to wherever it should go. In my case I just want it back to the class instance that owns the Server instance (which then wraps it in a Connection class and inserts it to a list).

  1. Server has a parameter in its constructor: std::function<void(socket)> OnAccept which is called in HandleAccept.
  2. I create an abstract class, IServerHandler or whatever, which has one virtual method OnAccept. Server takes IServerHandler as parameter in its constructor and the class instance owning the server instance extends IServerHandler and constructs Server with *this as parameter.

What are the pros and cons of option 1 vs option 2? Are there any better options? I'm having the same problem in my Connection class (OnConnectionClosed). Also, depending on how I decide to design the system, it might need a OnPacketReceived and OnPacketSent callback.

like image 626
simon Avatar asked Mar 12 '14 20:03

simon


People also ask

What pros mean?

adverb. in favor of a proposition, opinion, etc. noun, plural pros. a proponent of an issue; a person who upholds the affirmative in a debate. an argument, consideration, vote, etc., for something.

What is mean by pros and cons?

Definition of pros and cons 1 : arguments for and against —often + of Congress weighed the pros and cons of the new tax plan. 2 : good points and bad points Each technology has its pros and cons.

Are pros advantages?

The pros and cons of something are its advantages and disadvantages, which you consider carefully so that you can make a sensible decision.

Is cons positive or negative?

When weighing options, we use “pros” to describe positives while using cons to describe negatives. The idiom “pro and con” compares the advantages and disadvantages of something with the intention to aid in the decision-making process.


1 Answers

I strongly prefer the first way for several reasons:

  • Representing concepts/functionality via interfaces/class hierarchies makes the code base less generic, flexible, and then more difficult to mantain or scale in the future. That kind of design imposes a set of requirements on the type (the type implementing the required functionality) which makes it difficult to modify in the future, and most prone to fail when the system changes (Consider what happens when the base class is modified in this type of designs).

  • What you called the callback approach is just the classic example of duck typing. The server class only expects a callable thing which implements the required functionality, nothing more, nothing less. No "your type must be coupled to this hierarchy" condition is required, so the type which implements handling is completely free.

  • Also, as I said the server only expects a callable thing: It could be anything with the expected function signature. This gives the user more freedom when implementing a handler. Could be a global function, a bound member function, a functor, etc.

Take the standard library as an example:

  • Almost all standard library algorithms are based on iterator ranges. There is no iterator interface in C++. An iterator is just any type which implements the behaviour of an iterator (Being dereferenceable, comparable, etc). Iterator types are completely free, distinct, and decoupled (Not locked to a given class hierarchy).

  • Another example could be comparators: Whats a comparator? Is just anything with the signature of a boolean comparison function, something callable which takes two parameters and returns a boolean value saying if the two input values are equal (less than, bigger than, etc) from the point of view of a specific comparison criteria. There is no Comparable interface.

like image 157
Manu343726 Avatar answered Oct 08 '22 20:10

Manu343726