# Template Method
- A behavioral design pattern that defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure.
## **Real-World Analogy**

## **Problem**

A data mining application that analyzes corporate documents. Users feed the app documents in various formats (PDF, DOC, CSV), and it tries to extract meaningful data from these docs in a uniform format.
1. App could work only with DOC files.
2. It was able to support CSV files.
3. It support PDF files.

- A lot of similar code.
- Get rid of the code duplication.
- It had lots of conditionals.
- If all classes had a common interface or a base class, you’d be able to eliminate the conditionals in client code and use polymorphism when calling methods on a processing object.
## **Solution**
1. Break down an algorithm into a series of steps.
2. Turn these steps into methods.
3. Put a series of calls to these methods inside a single template method.

- Declare all steps abstract, forcing the subclasses to provide their own implementations for these methods.
- Code for opening/closing files and extracting/parsing data is different for various data formats. There’s no point in touching those methods.
- Implementation of other steps, such as analyzing the raw data and composing reports, is very similar, so it can be pulled up into the base class, where subclasses can share that code.
- Options: A hook is a step with an empty body. Usually, hooks are placed before and after crucial steps of algorithms, providing subclasses with additional extension points for an algorithm.
## **Structure**

1. Abstract Class declares methods that act as steps of an algorithm, as well as the actual template method which calls these methods in a specific order. The steps may either be declared abstract or have some default implementation.
2. Concrete Classes can override all of the steps, but not the template method itself.
## **Example**

## **Pros**
- You can let clients override only certain parts of a large algorithm, making them less affected by changes that happen to other parts of the algorithm.
- You can pull the duplicate code into a superclass.
## **Cons**
- Some clients may be limited by the provided skeleton of an algorithm.
- You might violate the Liskov Substitution Principle by suppressing a default step implementation via a subclass.
- Template methods tend to be harder to maintain the more steps they have.
## **Relations with Other Patterns**
- Factory Method is a specialization of Template Method. At the same time, a Factory Method may serve as a step in a large Template Method.
- Template Method is based on inheritance: it lets you alter parts of an algorithm by extending those parts in subclasses. Strategy is based on composition: you can alter parts of the object’s behavior by supplying it with different strategies that correspond to that behavior. Template Method works at the class level, so it’s static. Strategy works on the object level, letting you switch behaviors at runtime.