β delegate and event IMPLEMENTATION question..
why do we have to create separate class for delegates/events?
31 Replies
Wdym class?
A delegate is a delegate
ive noticed that people declare delegate in separate class
like here
delegate (with event) in separate class, other classes', main function in other class
Well typically you declare delegates in the same class that you declare the event in, if the delegate is tightly connected to the event specifically. If you look at
Func<>
and Action<>
though, they're just defined inside the System
namespace.well, why, do we declare event and delegate in separate class?
why wouldnt you make it global?
Well firstly you can't make an event global (you can make it static though)
why???
because events are type members
i.e. they have to be declared inside a type
where is it best to declare event?
And also because global events imply some kind of global state, which is generally bad
A)creating separate class for event/delegate
or
B)in the class which's object is connected to event
once again, dude literally created separate class for event, instead of implementing it in the Video class
(he had Video class which had only 1 property, just for sake of example)
btw, why wouldnt he implement Encode method inside Video class?
Doesn't seem like that class is only for the sake of the event
event and encoding**
you are correct
I assume
Video
is meant to contain data about a single video, so wouldn't make sense to have an event there????
what is wrong with:
why would he separate encode from the video class, that doesnt make any sense
What if you want to create a video but don't care about encoding it?
you can just dont use this method
What if the encoder needs some internal state?
wdym by internal state?
For instance, the encoder needs a list as a field to keep track of tasks or whatever.
If you put that in
Video
, then suddenly what is meant to just be data about a video contains state which isn't related to the video at all.oh
that makes sense π
ooorrr not
you can pass it as parameter
and dont include it in the properties (??)
yep, which generally leads to a lot cleaner and more maintainable code
The video doesn't have to worry about the logic of encoding, that's up to another class.
so, creating this methodo in the video class is better?
no, again separating it into another class is generally better
And besides, I have no idea why
Encode
is void
and for some reason needs an event. If I was making this this is certainly not how I would do it, but then again it's just for demonstration.nooo
i mean, passing external data in the parameters, instead of using it as properties
it's just and example
alright, in conclusion, i have to declare delegate / event inside the class that calls delegate / event?
nooo man, keep typing
One idea you'll see talked about is "Single Responsibility Principle". You want a class to do one thing: https://www.freecodecamp.org/news/solid-principles-single-responsibility-principle-explained/#:~:text=The%20Single%20Responsibility%20Principle%20(SRP,only%20one%20reason%20to%20change%22.
freeCodeCamp.org
SOLID Definition β the SOLID Principles of Object-Oriented Design E...
The SOLID design principles help us create maintainable, reusable, and flexible software designs. Each letter in the acronym SOLID stands for a specific principle. Here is what each letter in the acronym stands for: * S: Single responsibility principle. * O: Openβclosed principle. * L: Liskov
You don't have to declare the delegate in the same type as the event, although if the delegate is specifically meant for the event then it's a good idea to keep everything in one place.
okie, thanks!
are principles and patterns same thing?
Not really but there is some overlap. I wouldn't get hung up on how they are named.
If I had to distill the differences I would say principles are a higher level concept, patterns are how you implement the principles in your programming language of choice.
understandable, thanks!