Spiral Hero
Spiral Hero
CC#
Created by Spiral Hero on 10/18/2024 in #help
I cannot understand the command pattern.
I was studying patterns and no matter how much I try I can't comprehend this one. Been days of researching and something just doesn't make much sense about it. I will try to give the definition of it that I know of, and all of my questions about it. If anything in the definition itself is wrong, please let me know too!
The command pattern separates the command of a method in it's own object, and is used to solve a few scenarios on your code, which includes making an undo/redo function of the commands or just storing those commands in general, or delaying those methods on the code. The pattern consists on having an interface (also called Abstract command), a Concrete command class, an Invoker class that takes the command request and calls the command, a Receiver class and the Client that is requesting/trying to call the method from the receiver.
What I don't understand: 1- Since you can do the undo/redo or just delay the commands without it, I'm struggling to see how this helps 2- The receiver class having all the actual implementations sounds counterintuitive to me, doesn't this make one implementation bound to the others? (I wasn't sure whether this is a begginer or intermediate question, but I'm definitely a begginer)
15 replies
CC#
Created by Spiral Hero on 10/2/2024 in #help
Dependency Injection question
I was learning about coupling/decoupling and one thing that was confusing to me was this: Basically, people tell you to leave the instances for the calling of the constructor method, and instead just use an Interface that implements that class when using it on your code. The reason people will say this is useful includes (while not limited to) "Making so that if the signature of that class changes, the code using that class won't be affected, since it's using an abstraction of it, an Interface. Making it easier to change the class without breaking the code" While I understand that, doesn't this just changes the necessity of getting the signature correct from inside the code to "outside" the code? (And what I mean by that is calling the code, since you will need to pass the instance of this class anyways and if the signature has changed I don't see how it won't give errors the same exact way)
75 replies
CC#
Created by Spiral Hero on 5/30/2024 in #help
Struggling with one thing about interfaces.
So, I watching a tutorial about IEnumerable and I realized I don't really understand how interfaces work. What I thought they were: Similar to classes as a level of abstraction, but they normally don't have any default method and instead just have all the methods that NEED to be implemented by their child classes instead. They also can't be instantiated as objects (but objects of classes that implement them can) However, I ran into an example close to this: EXAMPLE: List<MyClass> MyList = new List<MyClass> {...}; ImyInterface<MyClass> ModifiedList = MyList (Suppose that ImyInterface is an interface and that MyClass is a class that implements ImyInterface) Here, the interface name (alongside with the generic indicator <>) is put right before an object named ModifiedList. Which made me think that ModifiedList was an object of the class ImyInterface... What I don't understand is: First: That interfaces in theory were supposed to not have objects instantiated directly from them Second: Even if they could, what is the point of doing this instead of directly using MyList, if MyClass already implements all the ImyInterface methods... When I asked chat gpt (yeah, Im THAT desperate), it told me that actually ModifiedList is an variable of type ImyInterface, which is possible because variables are not the same as objects, and interfaces can't be objects but they can be variables. Now, I'm not sure if chat gpt is bullshitting me, so I was left unsure, since I was taught that variables were a simplified term, but are actually objects internally too. Which is why I was doubting the AI explanation. So, regarding the 2 questions I made above, what is the correct explanation? Sorry for the wall of text, I'm really confused.
73 replies