Reputation: 813
How can i do this:
class BaseClass{
void templateMethod(){
methodImplementation();
if(this.base.getType() != typeof(BaseClass)) this.base.methodImplementation();
}
void abstract methodImplementation();
}
class Derived1 : BaseClass{
void override methodImplementation(){
.. do things ..
}
}
class Derived2 : Derived1{
void override methodImplementation(){
.. do more things ..
}
}
This would then result in both implementations (things AND more things) being executed.
Derived2 object = new Derived2();
object.templateMethod();
Then all children and grand children and so on, would only need to implement their part of methodImplementation and it would get called throughout all levels when called in the final object.
But this.base is not a thing. What I need is to call the implementation of the parent of the instantiated object real class, but the code is in the baseClass so I can use it in a Template method pattern.
Thanks in advance, sorry if I'm not making myself clear. I just need to know if it can be done. This being good practice, optimal, recommended or useful is another thing that I'll consider.
EDIT:
The code above is not exactly right, it should be like this:
class BaseClass{
void templateMethod(){
methodImplementation();
if(this.base.getType() != typeof(BaseClass)) this.base.templateMethod();
}
void abstract methodImplementation();
}
class Derived1 : BaseClass{
void override methodImplementation(){
.. do things ..
}
}
class Derived2 : Derived1{
void override methodImplementation(){
.. do more things ..
}
}
the difference being that in BaseClass.templateMethod() what gets called in the second line is templateMethod(), not methodImplementation(), so it calls itself but from the "perspective" of the parent class. Now I'm thinking more and more this is just not possible and creates more confusion than anything.
Upvotes: 0
Views: 154
Reputation: 813
OK, even if there is a convoluted way to get to do that, it wouldn't even be of use for what I need, and I guess it's no use for any kind of decent code.
Upvotes: 0
Reputation: 6155
You can call the base method in your derived classes. Here's an example. You can call the base method in each line of your derived class; depending on when you want to execute the logic.
public class Animal
{
public void WhatAmI()
{
Console.WriteLine("Animal");
}
}
public class Fish : Animal
{
public new void WhatAmI()
{
base.WhatAmI();
Console.WriteLine("Fish");
}
}
public class HammerheadShark : Fish
{
public new void WhatAmI()
{
base.WhatAmI();
Console.WriteLine("HammerheadShark");
}
}
When you call the WhatAmI
method:
var animal = new HammerheadShark();
animal.WhatAmI();
You'll get this:
Animal
Fish
HammerheadShark
Upvotes: 0
Reputation: 1209
If I understand you correctly, you want to call all methodImplementation
from top level derived class. You can do it like this:
class BaseClass{
void templateMethod(){
methodImplementation();
}
void abstract methodImplementation();
}
class Derived1 : BaseClass{
// virtual as you want to derive it again
virtual void override methodImplementation(){
//.. do things ..
}
}
class Derived2 : Derived1{
void override methodImplementation(){
//.. do more things ..
base.methodImplementation();
}
}
Each derived class only has to implement his part of methodImplementation
and at the end call their base class. Since methodImplementation
is abstract
dont call base
on first level. Also mark all methodImplementation
as virtual so you can override it again.
Upvotes: 1