C
C#11mo ago
Tueem

Unsure on how to structure using OOP

Hey, I have written the following abstract class to abstract away different communication drivers like serial or udp/tcp and there is already a feature which sends ping messages and checks for a response to see if there is something on the other end of the port. But I think there is a better way to do this using more than one class. I also want to add a port connected check for when a usb gets yanked for example. How would you guys structure that? I'm also open for criticism about my already written code! This is the code https://pastebin.com/iQBTHVjG
Pastebin
using CommandWingDriver.Event;using CommandWingDriver.IO.Implementa...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
5 Replies
Omnissiah
Omnissiah11mo ago
not everything requires a Init/Stop (and a ping), but that's a matter for you to decide** did you use Init because it's 4 letters like Stop? why is everything async except read and write (ah i guess because of the events) * which in some cases it's called Are You There ** maybe put a feature report? also i would put an event field for start/stop because you could have the need to act on it for example if error happens you could have external logic to restart it but maybe you have built a chain of these classes and need a way to synchronize then * i wouldn't use an actual event, just action/func, but again your code third, i wouldn't use a generic T because low level would always send/recv bytes the "higher" parts that manage actual objects would be different from this class still, it depends on really what role these classes have in your service if you have just one or two then probably who cares
Tueem
TueemOP11mo ago
almost all of my stuff needs init/stop but with ping youre correct only some need a ping, thats because i want to make it optional using another class in some way but im unsure how exactly I would do it. I just used init because I didnt want there to be Start() and StartDriver(). what do you mean by feature report? I had it using just bytes at first but I have some drivers which just receive color values so I figured it would be more useful to have it with T.
Omnissiah
Omnissiah11mo ago
only some need a ping,
there are the usual ways to do this command could be implemented in a third class, command is optional and throws if not present, command is a message in a endpoint for commands (my preferred way) StartDriver would be protected, so it wouldn't be that much of a problem... but maybe this could be a sign that the class should be in some way splitted feature report in the sense that a method could enum what commands are supported, for example it could return a list of string, a flag, whatever so it could tell externally if it needs to be started/stopped, if it has ping, and so on; kinda low level, but hey it works for a lot of people to transmit a color you don't need to serialized it into bytes?
Tueem
TueemOP11mo ago
problem is that there is no "external" the class should manage itself and not need some manager class. But im still gonna try to make something up and show it to you afterwards if thats okay
Omnissiah
Omnissiah11mo ago
but the class could be the client itself nobody says the client can't have layers (or rather, be a pipeline)
Want results from more Discord servers?
Add your server