Bohn
Bohn

Reputation: 26919

Creating Abstract methods when only Part of the method is the same in all classes

I have a bunch of classes that I am trying to refactor and abstract their common member variables and methods.

One method that I see if in every class is a method called MethodFoo(). This method has a Common body but each class has added something MORE to that..so for example for one class it is like this:

private method MethodFoo()
{

   // common stuff ...// wow! a Goto Statement ..seriously!  //1

   **// NON-common stuff just for the method of this class**  //2

   Goto: // bunch of COMMON stuff again   //3
}

so the template is like always parts 1 and 3 are the same for this method in different classes BUT what is different for it in eaxch class is part 2 ... and also don't forget that ugly goto statement :)

So with this picture in mind, do you thikn is there a way I can put some part of this in my abstract class?

Upvotes: 0

Views: 92

Answers (4)

leqid
leqid

Reputation: 931

This can be solved using the "Template Method" pattern. See "Form A" here:

Naming convention for non-virtual and abstract methods

You can refactor all the MethodFoo's to "pull up" the common stuff into a base class. Add to the base class a method "protected virtual MethodFooInternal" (similar to the "WalkInternal" of the example above). Derived classes will implement their non-common stuff in MethodFooInternal.

To eliminate the GOTO, you could create conditional logic in the base class to decide whether to call MethodFooInternal or not.

private method MethodFoo()
{

   // common stuff ...// wow! no more Goto Statement   //1

    if (this.needToExecuteNonCommonStuff()) {
       **// NON-common stuff just for the method of this class**  //2
        MethodFooInternal();
    }

   // bunch of COMMON stuff again   //3
}

protected abstract void MethodFooInternal();
}

Upvotes: 3

Michael G
Michael G

Reputation: 6745

Make it a protected method, and your derived class can override it and call the base implementation:

protected override void MethodFoo()
{

  base.MethodFood();

   **// NON-common stuff just for the method of this class**  //2
}

This of course requires knowing the order that the steps need to happen in the derived classes. A better approach would be to use the answer provided by Austin Salonen, or Oded.

Upvotes: 1

Austin Salonen
Austin Salonen

Reputation: 50225

You're describing the Template Method Pattern.

Implementers handle the DetailStep and but ToDo is common to all.

public abstract class Parent
{
    public void ToDo()
    {
        Pre();

        DetailStep();

        Post();
    }

    protected abstract void DetailStep();
}

Upvotes: 3

Oded
Oded

Reputation: 499092

In your base class:

private method MethodFoo()
{

   // common stuff ...// wow! a Goto Statement ..seriously!  //1

   DoExtraStuff();

   Goto: // bunch of COMMON stuff again   //3
}

protected abstract void DoExtraStuff();

This way, you must implement DoExtraStuff in all your inheriting classes.

The pattern is known as the Template Method design pattern.

Upvotes: 7

Related Questions