karljj1 / kdis

DIS(IEEE 1278.1) Fully Implemented in C++. A complete open source implementation of DIS (Distributed Interactive Simulation) in C++. 1278.1 and 1278.1a are fully implemented including enumerations. Includes several utility classes to help with developing your DIS app.
BSD 2-Clause "Simplified" License
26 stars 10 forks source link

Connection builder pattern #11

Open carlocorradini opened 1 year ago

carlocorradini commented 1 year ago

Using the current constructor to create a Connection class is a little too verbose and tedious. Additionally, although if some options can be modified after creation, some cannot (port). I propose to modify the Connection class's creation process by utilizing the Builder Pattern:

const Connection connection = Connection::builder("1.2.3.4") // Automatically determine if address is multicast or not
                                 .port(3000) // Send/Receive port
                                 .interface("eth0") // Send/Receive interface
                                 .timeout(3, 0) // Send/Receive timeout
                                 .blocking(true)
                                 .factory(myFactory)
                                 .receiveAddress("1.2.3.4")
                                 .receivePort(12000)
                                 .receiveInterface("eth0") // Interface name
                                 .receiveTimeout(6, 0)
                                 .sendAddress("4.3.2.1")
                                 .sendPort(6000)
                                 .sendInterface("9.9.9.9") // Interface IP
                                 .sendInterface(0) // Interface index -> 0 is default interface
                                 .sendTimeout(12, 0)
                                 .receive(false) // Disable receive -> If both receive/send (after .build() call) are false throw error
                                 .send(false) // Disable send -> If both receive/send (after .build() call) are false throw error
                                 .addSubscriber(S0)
                                 .removeSubscriber(S0)
                                 .build();

connection.connect();
// ...
connection.disconnect();

What do yuo think? 🥳

karljj1 commented 12 months ago

Looks interesting. How much code would this require?

carlocorradini commented 12 months ago

Not that much since it's a standard pattern where the majority are setters functions