简体   繁体   中英

OOP convert child to parent and back

I have a general question about OOP I have found the following code in a program (shown as an example). Here a parent class is expected as a parameter, but a child is passed, which is then converted from the parent type back to the child type I am the one who thinks that this kind of back and forth conversion is against the OOP rules or not?

private void OnSend (BaseParameters obj) {
    var request = commandBuilder.BuildCommand (obj);
    _controller.ReceiveRequest (request);
 }

 public BaseCommand BuildCommand (BaseParameters parameters) {

    switch (parameters) {
       case ParametersCopy parametersCopy: //Give a base parameter and cast to spezific child parameter 
          {
             return = new CommandCopy (parameters.XY.parameters.ZX)
          }
       case ParametersDelete parametersDelete:
          {
             return new CommandDelete (parameters.XY.parameters.ZX);
          }
    }
 }

 //Await a Base command but a child command is passed
 public void ReceiveRequest (BaseCommand cmd) {
    CommandQueue.AddCmd (cmd);
 }

 public void ReceiveEndlessRequest (BaseCommand cmd, CancellationToken cancellationToken) {
    while (!cancellationToken.IsCancellationRequested) {
       var newPram = cmd.Clone ();
       ReceiveRequest (newPram);
    }
 }

This code is still working, because C# only give a refrenze to the object. So you can convert them from parent co child and back.

My problem is that if I give a pure base command in "ReviceRequest", my application crashes, because the specific command is used internally

My "ReviceEndlessRequest" function does not work anymore. Since the copy is executed on the base types, all child properties are lost. But since "ReviceRequest" expects the base type everything seems to be ok. But the application supports it because parameters are missing.

In my opinion this is a violation of OOP rules. I expect a BaseType and the application works internally with the childType

Casting the parameters in the BuildCommand is also wrong in my opinion. How do you see that?

Casting a base type to a child type is perfectly acceptable as long as you test that it is the appropriate type first, which is what the switch statement in your example is doing.

There should be a default case in the switch that handles if the parameter is not a known child type.

In a perfect world, you might not need to use this pattern, because all the specialized behavior of subclasses is accessed through polymorphic methods defined on the base. What often happens as real systems evolve is that you need some specialized behavior of known subclass types, but can't change the base type.

The behavior that you see in your code is due to polymorphism. As you know polymorphism states that one thing has many forms. so in your case, CopyCommand or DeleteCommand is one of the forms of Command.

BaseCommand does cause an error because it is an abstract form of command. It has any abstract method which got implemented by its child classes.

BuildCommand method is a kind of Factory Method, which construct your command based on input your provide.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM