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).
Server
has a parameter in its constructor: std::function<void(socket)> OnAccept
which is called in HandleAccept
.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.
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.
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.
The pros and cons of something are its advantages and disadvantages, which you consider carefully so that you can make a sensible decision.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With