Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I've read about Template Method Pattern but I'm not sure about one thing.

The steps (methods) of an algorithm are supposed to be in the template method. In the case my template method's algorithm is about 2 completely different tasks that I would like all my objects to perform, let's say the one is drawing and the other calculating, can I still put all these methods in one method and call this my template method?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

It is hard to tell without more information but methods are meant to perform a single operation. If it is doing two related but different things, each thing should be moved into a separate method. There is nothing wrong with having multiple template methods, particularly if one thing may want to be modified (that is overridden) but not the other.

Moreover, if the class is doing multiple things (such as drawing and calculating), it may violate the "Single Responsibility Principle", however this digresses from the question.

share|improve this answer

There is nothing really that prohibits you from calling two things from the template method. In your case you could do it like this:

public abstract class ContextObject {

    // The template method
    public void PerformDraw() {
        Draw();
        Calculate();
    }

    public abstract void Draw();

    public abstract void Calculate();

}

// The implementation is rather obvious:

public class ConcreteObject : ContextObject {

     public void Draw() { ... }

     public void Calculate() { ... }

}

// For all the concrete objects you only need to call 
// the template method, regardless of the implementation

for (ContextObject context : contextObjectList) {
    context.PerformDraw();
}

The only drawback with this template object class is that everyone needs to implement the Draw and the Calculate methods. So if all subclasses need to mix between draw and calculate in several combinations you might want to use strategy pattern instead like this:

// Strategy #1
public interface IDrawable {
    void Draw();
}

// Strategy #2
public interface ICalculatable {
    void Calculate();
}

// the previous class needs to use the interfaces above:
public abstract class ContextObject {

    private IDrawable drawable;
    private ICalculatable calculatable;

    public ContextObject(IDrawable drawable, ICalculatable calculatable) {
        this.calculatable = calculatable;
        this.drawable = drawable;
    }

    public void PerformDraw() {
        drawable.Draw();
        calculatable.Calculate();
    }

}

// The implementations
public class DrawUno : IDrawable { ... }
public class CalcUno : ICalculatable { ... }

public class ConcreteObject : ContextObject {

    // call the base class's constructor with the implementations
    public ConcreteObject() : 
        base( 
            new DrawUno(), 
                // can be mixed with any other of type IDrawable
            new CalcUno()
                // can be mixed with any other type of ICalculatable
        ) 
    {
        // no-op, the base constructor does the stuff you need...
    }

}

You could also mix and match, it all depends on what you actually need to do.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.