Lets say I have a class, myClass that has two methods - addOne() and addTwo().
I have another class, adding. This class has an instance boolean variable, oneOrTwo. This variable decides whether to use addOne() or addTwo() on a myClass object.
I have already written all the lines of code that use the addOne() method and there are a lot of lines. What I don't want to do, is replicate them, something like this:
if (oneOrTwo)
{
// all lines of code for addOne()
}
else
{
//all lines of code for addTwo()
}
The lines of code for addOne() includes a lot of myClass.addOne() calling (of course I have created a new object and everything works perfectly, but I'm speaking generally here to avoid making this an unnecessarily big post). My question is if I can do something like:
if (oneOrTwo)
addNumber() = addOne();
else
addNumber() = addTwo();
And then, instead of using myClass.addOne() or myClass.addTwo(), I can use myClass.addNumber().