-2

Ok, so I've already solved this but I want to ask/share what you guys think.

I have a base class with 2 overloads for a method, for instance

public class Entity{
    public virtual bool Save() {
          //ReallySave()...
    }

    public virtual int Save(string someParam){
       //also ReallySave()...
    }
}

and I have a derived class, in which I would like to override whatever save() method I feel like overriding, for instance

class Person {
     override int Save(string someParam){
           console.log(someParam)
           base.Save(someParam);
     }
}

Now the problem is, somewhere else, I wan to call

person.Save();

or

 person.Save("hello");

but I have no way to know (nor should I) which overload is overriden. This code falls due to its combination of overridable overloaded methods, since calling save() would not go into the overriden method.

So I did some trickery to make sure, at the base level, each base method calls the other, something like the "chain of responsability" pattern.

An example of the modifyed base class would be

public class Entity{

    private bool WithParamsMethodCalled = false;
    private bool NoParamsMethodCalled = false;
    public virtual bool Save() {
          NoParamsMethodCalled  = true;
          if(!WithParamsMethodCalled ) { Save(""); }
          //ReallySave()...
    }

    public virtual int Save(string someParam){
           WithParamsMethodCalled = true;
          if(!NoParamsMethodCalled ) { Save(); }
          ///no need to call ReallySave() here
    }
}

Now the question: Is there a better way to do this? The code works but

a. its smelly and

b. If i want more than 2 overloads it gets uglier

thanks!

user5328504
  • 734
  • 6
  • 21
  • What about having one method with an optional parameter? Or having one common private method in the base and then you can get rid of your booleans? – ProgrammingLlama Mar 21 '19 at 02:53
  • the problem with optional parameters is (in the real life case) these calls can really differ parameter wise and also differ in return types! so its not just about having "more options". Secondly, the common private method ReallySave() does exists and still doesnt aid in making sure your derived class override is called – user5328504 Mar 21 '19 at 02:59
  • Are you complaining that person.Save(string) calls entity.Save() even though person overrides Save()? You DID state base.Save() in person.Save(string); the runtime is only doing as it's told, no? Or were you expecting that referring explicitly to entity.Save() via base.Save() on person would follow a logic of "trying entity.Save()... oh, it's overridden, I'll call the overridden version in person.Save() instead"? – Caius Jard Mar 21 '19 at 03:08
  • I'm saying and instaced "person" object exposes Save() and Save(string) and any could be overriden in Person. You want to make sure the implemented one is called no matter if you call Save() [not implemented] or Save("") [implemented] – user5328504 Mar 21 '19 at 03:12

1 Answers1

-1

Since I have many overridable methods all with the same name, I created a small pattern to keep track of which override was used (in base.MyMethod(). Then, the user can call any of the overloads and the app will cycle through all overloads until it sees there's one who set its "executed" flag to true. Then execute the "real base method" and break the loop. I guess this pattern could be described as "chained overloads for single override with indistinct entry point"

E_net4
  • 27,810
  • 13
  • 101
  • 139
user5328504
  • 734
  • 6
  • 21