2 different tasks in template method

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?

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.

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật