Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is template method design pattern an example of Inversion of control?

Recently in a interview I was explaining about a framework that i have worked on. I said that we created a inversion of control by providing extensiblity using template method design pattern. I said this was an example of Inversion of Control where our framework was calling the methods implemented by the user of framework, to which interviewer said that a template method design pattern is not an example of IOC. I wonder if my understanding of IOC is incorrect ?

like image 564
naveen Avatar asked May 16 '14 03:05

naveen


People also ask

Which design pattern is used in inversion of control?

Callbacks, schedulers, event loops, dependency injection, and the template method are examples of design patterns that follow the inversion of control principle, although the term is most commonly used in the context of object-oriented programming.

What is inversion of control with example?

Inversion of Control (IoC) is a design principle that allows classes to be loosely coupled and, therefore, easier to test and maintain. IoC refers to transferring the control of objects and their dependencies from the main program to a container or framework.

What is template method pattern in design pattern?

Template method design pattern is to define an algorithm as a skeleton of operations and leave the details to be implemented by the child classes. The overall structure and sequence of the algorithm are preserved by the parent class. Template means Preset format like HTML templates which has a fixed preset format.

Which of the following is a software design pattern which is a technique for achieving inversion of control between classes and their dependencies?

Dependency injection is a pattern we can use to implement IoC, where the control being inverted is setting an object's dependencies. Connecting objects with other objects, or “injecting” objects into other objects, is done by an assembler rather than by the objects themselves.


2 Answers

Your interviewer was wrong. The template method pattern does use inversion of control. In fact, the Wikipedia entry specifically mentions it.

http://en.wikipedia.org/wiki/Template_method_pattern

The template method is used in frameworks, where each implements the invariant parts of a domain's architecture, leaving "placeholders" for customisation options. This is an example for inversion of control, also called the Hollywood principle.

Unfortunately, you either didn't understand IoC or the Template method pattern well enough to explain to them why it was an example of IoC. Unfortunately, many people seem to think IoC means Dependency Injection, and that's it.

like image 120
Erik Funkenbusch Avatar answered Nov 03 '22 18:11

Erik Funkenbusch


Yes, template pattern is an example of IOC and IOC can be achieved using Template pattern as well along with few other techniques(DI etc.). In inheritance child classes call the methods from parent classes but using template pattern we define an algorithm (sequence of steps which cannot be changed by child classes) using a final method implementation in base class as per shown in example mentioned below, Base/Parent class is calling methods which will be defined in child classes so control is inverted and base class has the control over the core algorithm, so this is why and where IOC is achieved in this case.

Example - Lets say we need to process a file.

public abstract class FileProcessor {
    public final void processFile() {
        preProcess();
        process();
        postProcess();
    }

    public abstract void preProcess();
    public abstract void process();
    public abstract void postProcess();
}
like image 38
user10916892 Avatar answered Nov 03 '22 19:11

user10916892