Fact-checked by Grok 2 weeks ago

Template method pattern

The Template Method pattern is a behavioral design pattern in that defines the skeleton of an algorithm in an operation of a superclass, deferring some steps to subclasses while keeping the overall structure invariant. This pattern enables subclasses to customize specific steps of the algorithm without altering its high-level , promoting and flexibility in . Introduced in the seminal 1994 book Design Patterns: Elements of Reusable by , Richard Helm, Ralph Johnson, and John Vlissides (commonly known as the or GoF), it embodies the "Hollywood principle" by allowing higher-level components to dictate control while inverting dependencies on lower-level details. The pattern's structure typically involves an abstract base class containing a template method—a non-overridable operation that orchestrates the by invoking a series of abstract or concrete primitive operations, some of which subclasses may override. It is applicable in scenarios where an algorithm has parts that should remain fixed across variations, such as in application frameworks, pipelines, or GUI event handling, where common steps like initialization or cleanup are shared but specific behaviors differ. For instance, in .NET frameworks like , base control classes use this pattern to define lifecycle methods, allowing derived custom controls to override rendering or child creation steps while reusing core logic for and event handling. Key benefits include eliminating code duplication by centralizing common algorithm logic in the superclass, ensuring a consistent for subclasses, and facilitating extensibility without modifying existing , aligning with the open-closed . However, it can introduce rigidity if the algorithm's skeleton becomes too prescriptive, potentially violating the if subclasses cannot fully adhere to the base behavior, and may complicate maintenance in complex hierarchies with many customizable steps. Overall, the Template Method pattern remains a foundational in for balancing abstraction and customization in reusable designs.

Definition and Motivation

Definition

The template method pattern is a behavioral design pattern that forms part of the 23 classic patterns cataloged by the (GoF) in their 1994 book Design Patterns: Elements of Reusable Object-Oriented Software, authored by , Richard Helm, Ralph Johnson, and John Vlissides. This pattern belongs to the behavioral category, which focuses on algorithms and the assignment of responsibilities between objects. At its core, the template method pattern defines the skeleton of an algorithm within a single operation, known as the template method, while deferring the implementation of specific steps to subclasses. This approach ensures that the overall structure and sequence of the algorithm remain invariant, even as subclasses provide customized behavior for individual primitive operations. The pattern leverages to allow subclasses to override and specialize these primitive operations without modifying the enclosing algorithm's flow, promoting and flexibility in object-oriented systems. It was formally defined and popularized through the GoF publication.

Motivation

In , many algorithms share a fixed high-level structure composed of invariant steps that must always execute in a specific order, but include variable sub-steps that differ based on context or requirements. Without a mechanism to enforce this structure while permitting customization, developers often implement these algorithms separately in multiple classes or subclasses, resulting in substantial code duplication and maintenance challenges. The template method pattern solves this problem by providing a "" in a base class that outlines the algorithm's , deferring the variable sub-steps to subclasses for redefinition without altering the overall . This approach ensures in the algorithm's execution while allowing flexibility for specialized behaviors, such as in frameworks where core operations remain stable but extensions vary. A typical scenario arises in application frameworks, such as a drawing application and a application, where opening a involves fixed steps like checking if it can be opened, creating the object, and reading its contents, but these steps vary by application type. By centralizing the invariant steps, the eliminates redundant code across implementations for different applications. This design promotes by factoring common logic into a single location and preserves algorithm integrity as systems evolve, reducing errors from inconsistent implementations and facilitating easier extensions through subclassing.

Structure and Components

Key Components

The template method pattern revolves around an AbstractClass that serves as the foundation for the algorithm's structure. This class declares the template method, which outlines the fixed sequence of steps, and defines primitive operations—either as abstract methods requiring implementation by subclasses or as concrete methods providing default behavior that subclasses may override. Subclasses, known as ConcreteClass, extend the AbstractClass and supply specific implementations for the abstract primitive operations, thereby customizing the algorithm without altering its overall skeleton. These concrete classes must implement all abstract steps but are typically prevented from modifying the template method itself to preserve the algorithm's integrity. The template method is a key non-virtual (often final) operation within the AbstractClass that encapsulates the algorithm's high-level steps by invoking the primitive operations in a predefined order, ensuring the skeleton remains unchanged across implementations. This design enforces that subclasses adhere to the established flow while varying only the designated extension points. Primitive operations represent the customizable elements of the algorithm, functioning as hooks or abstract methods that allow subclasses to inject their logic; they include abstract primitives that must be overridden, concrete primitives with optional overrides for default behavior, and hook methods that provide empty implementations for further extensibility if needed. A critical distinction in the pattern is between the final template method, which cannot be overridden to safeguard the algorithm's structure, and the overridable primitive operations, which enable variation without compromising the overall design.

UML Class Diagram

The standard UML class diagram for the Template Method pattern features an abstract class, typically named AbstractClass, which serves as the base for subclasses. This class includes a template method, such as templateMethod(), depicted as a public operation that orchestrates the algorithm by invoking a series of operations like primitiveOperation1() and primitiveOperation2(). Additionally, AbstractClass may contain operations providing default implementations for certain steps. Extending from AbstractClass are one or more concrete classes, such as and , represented as specialized subclasses. These concrete classes implement the abstract primitive operations defined in the parent class, ensuring the algorithm's steps are customized without altering the overall structure. The does not show implementations for the template method in these subclasses, as it remains fixed in the abstract class. The primary relationship in the diagram is generalization, illustrated by solid lines with hollow triangle arrows pointing from ConcreteClassA and ConcreteClassB to AbstractClass, denoting . No additional associations, dependencies, or compositions appear beyond this hierarchical structure, emphasizing the pattern's reliance on subclass extension. Variations in notation may include italicizing abstract operations to distinguish them from concrete ones, per standard UML conventions. These elements highlight flexibility in the algorithm skeleton while maintaining the pattern's core intent. The diagram visually embodies the Hollywood Principle—"don't call us, we'll call you"—through the template method's deferred invocation of subclass-specific primitive operations, inverting control so that the abstract class dictates the call sequence rather than subclasses initiating it.

Implementation Guidelines

Algorithm Skeleton

The algorithm skeleton in the Template Method pattern forms the core of the design, residing in an abstract superclass that defines the unchanging sequence of steps for an algorithm while deferring specific implementations to subclasses. This skeleton is realized through a dedicated template method, which serves as the orchestrating operation invoking a series of primitive operations in a fixed order, thereby enforcing the algorithm's structure without allowing alterations to its overall flow. To construct this skeleton, the process begins by declaring abstract methods for the primitive operations—those variable steps that must be implemented by subclasses to provide algorithm-specific . If certain steps represent logic applicable across all subclasses, implementations of these operations can be provided directly within the abstract class. The template method is then defined, often marked as final to prevent overriding, ensuring the algorithm's immutability; a typical structure might follow the form templateMethod() { step1(); step2(); step3(); }, where each step corresponds to a primitive or operation. This orchestration centralizes the algorithm's , allowing subclasses to customize only the designated primitives without disrupting the sequence. Immutability of the algorithm order is preserved by restricting the template method's accessibility and modifiability, which safeguards against subclasses inadvertently or maliciously altering the execution path and thereby breaking the intended behavioral . Designating the template method as final or protected achieves this, as it blocks overrides while still permitting internal use within the . Protected access modifiers are essential for the operations within the , enabling subclasses to override and access these methods internally without exposing them to external clients, thus maintaining encapsulation and controlled extension of the algorithm. This modifier level strikes a balance between flexibility for and , preventing unintended dependencies outside the pattern's scope. A key best practice is leveraging the skeleton to encapsulate all invariant behavior in the abstract class, which minimizes code duplication by avoiding redundant implementations in subclasses and enhances maintainability through centralized algorithmic logic. This approach not only reduces development effort but also facilitates easier evolution of the common structure over time.

Hook Methods

In the template method pattern, hook methods are optional operations defined within the abstract class, typically implemented as empty or default concrete methods that provide predefined extension points for subclasses. These hooks allow subclasses to optionally override them to insert custom behavior at specific locations in the algorithm without disrupting the overall structure. Unlike primitive operations, which must be implemented by subclasses, hooks are designed to be non-mandatory, enabling a "no-operation" (no-op) default that preserves the template's integrity if no customization is needed. The primary purpose of hook methods is to enhance the flexibility of the template method pattern by accommodating conditional or additional logic in subclasses, thereby supporting extensibility while adhering to that the abstract class controls the algorithm's flow. By placing hooks strategically between or around primitive operations in the template method, designers can offer customization opportunities without forcing every subclass to implement unnecessary functionality, which promotes reusability and reduces . This approach aligns with the Hollywood principle, where the framework calls subclass methods rather than the reverse, ensuring that optional extensions integrate seamlessly into the orchestrated algorithm skeleton. A representative example of hook methods appears in a game loop template, where the abstract class defines a core sequence of steps like initialization, , and rendering, with an optional hook method such as pause() inserted before the update phase; subclasses for games requiring pausing functionality can override this hook to implement it, while others leave it as a no-op to skip pausing entirely. Guidelines for implementing hook methods emphasize simplicity and optionality: declare them with empty bodies in the abstract class to default to no-op behavior, use consistent like a "Do-" (e.g., doPreProcess()) for clarity, and position them only at points where extensions add value without complicating the core algorithm. Subclasses should override hooks judiciously to avoid introducing side effects that could alter the expected , ensuring the pattern remains lightweight and focused on skeletal definition.

Practical Applications

Framework Integration

The template method pattern plays a central role in software frameworks by allowing abstract classes in libraries to define the skeleton of key algorithms or workflows, while providing hook methods for subclasses to customize specific behaviors without altering the overall structure. In servlet frameworks, for instance, the HttpServlet class in EE implements this pattern through its service method, which orchestrates the handling of HTTP requests by dispatching to overridable methods like doGet and doPost based on the request type, enabling developers to extend request processing while adhering to a predefined sequence. This approach ensures that the framework maintains control over the invariant parts of the process, such as request validation and response formatting, while deferring user-specific logic to subclasses. A concrete example within the is the AbstractList class, which uses the template method pattern to provide a skeletal implementation of list operations. Methods like add(E e) and clear() are defined in terms of primitive operations such as the abstract get(int index) and overridable remove(int index), allowing subclasses like ArrayList to implement efficient element access and modification while reusing the framework's algorithmic structure for iteration and bulk operations. This integration offers significant benefits, including the ability to evolve frameworks over time without disrupting existing client implementations, as changes to the template skeleton can be made independently of hook customizations. It also promotes the principle, where the framework calls user code rather than vice versa, fostering reusable and extensible architectures. Historically, the template method pattern has been prevalent in (GUI) frameworks since the 1990s, particularly for component lifecycle management; for example, in Java's AWT and libraries, base classes like JComponent define the overall painting and layout flow while allowing subclasses to override specific steps.

Code Generation Scenarios

The Template Method pattern finds significant application in tools, where it structures the algorithm for producing while permitting customization for specific requirements, such as target programming languages or domain models. In these scenarios, an base class outlines the high-level steps of the generation process—typically including , core structure , and output formatting—with primitive operations left for subclasses to implement language-specific details like syntax generation or type mappings. This ensures a consistent across diverse outputs, such as XML classes or database entity models, without duplicating the overarching logic. These implementations align with the pattern's core intent of encapsulating invariant aspects of code production in the template method. One key advantage of applying the Template Method pattern in is the reduction of manual boilerplate coding, as the fixed algorithm skeleton promotes reuse and enforces structural consistency across generated outputs, thereby minimizing errors in repetitive tasks. Additionally, it facilitates extensibility through subclassing, enabling developers to tailor primitives without altering the core process, which is particularly valuable in multi-language or multi-framework environments. However, the pattern's reliance on inheritance limits its suitability for highly dynamic environments, where runtime adaptability is needed over static generation; it excels in compile-time or build-time scenarios but may introduce rigidity if frequent algorithmic changes are required.

Examples

Java Implementation

The template method pattern in is commonly realized through an abstract class that encapsulates the algorithm's structure, defining a concrete template method that orchestrates calls to abstract primitive operations implemented by subclasses. This approach leverages Java's support for to ensure the algorithm's steps remain while allowing of specific behaviors. To demonstrate, consider a data processing scenario where an abstract class DataProcessor defines a template method processData() that sequentially invokes abstract methods parse() and validate(). Concrete subclasses XMLProcessor and JSONProcessor provide implementations tailored to their respective data formats, such as parsing XML or JSON strings and performing format-specific validation.
java
import java.util.List;

// Abstract class defining the template method
abstract class DataProcessor {
    // Template method defining the algorithm skeleton
    public final void processData(String inputData) {
        List<String> parsedData = parse(inputData);
        validate(parsedData);
        // Additional fixed steps could follow, e.g., save or log
        System.out.println("Data processing completed successfully.");
    }

    // Abstract primitive operation: subclasses must implement
    protected abstract List<String> parse(String inputData);

    // Abstract primitive operation: subclasses must implement
    protected abstract void validate(List<String> parsedData);
}

// Concrete subclass for XML processing
class XMLProcessor extends DataProcessor {
    @Override
    protected List<String> parse(String inputData) {
        // Simulated XML parsing (in practice, use a library like JAXB or DOM)
        System.out.println("Parsing XML data: " + inputData.substring(0, Math.min(20, inputData.length())) + "...");
        return List.of("XML Element 1", "XML Element 2"); // Mock parsed elements
    }

    @Override
    protected void validate(List<String> parsedData) {
        // Simulated XML validation (e.g., check for required tags)
        if (parsedData.size() < 2) {
            throw new IllegalArgumentException("Invalid XML: Insufficient elements");
        }
        System.out.println("XML validation passed for " + parsedData.size() + " elements.");
    }
}

// Concrete subclass for JSON processing
class JSONProcessor extends DataProcessor {
    @Override
    protected List<String> parse(String inputData) {
        // Simulated JSON parsing (in practice, use a library like Jackson or Gson)
        System.out.println("Parsing JSON data: " + inputData.substring(0, Math.min(20, inputData.length())) + "...");
        return List.of("JSON Key 1", "JSON Key 2"); // Mock parsed keys
    }

    @Override
    protected void validate(List<String> parsedData) {
        // Simulated JSON validation (e.g., check for required fields)
        boolean hasRequiredKeys = parsedData.stream().anyMatch(key -> key.contains("Key 1"));
        if (!hasRequiredKeys) {
            throw new IllegalArgumentException("Invalid JSON: Missing required key");
        }
        System.out.println("JSON validation passed for " + parsedData.size() + " keys.");
    }
}

// Main method to demonstrate polymorphism
public class TemplateMethodDemo {
    public static void main(String[] args) {
        DataProcessor xmlProcessor = new XMLProcessor();
        xmlProcessor.processData("<root><item>value1</item><item>value2</item></root>");

        System.out.println(); // Separator

        DataProcessor jsonProcessor = new JSONProcessor();
        jsonProcessor.processData("{\"key1\": \"value1\", \"key2\": \"value2\"}");
    }
}
When executed, the main method creates instances of the concrete processors polymorphically via the abstract class reference. Invoking processData() on each enforces the fixed sequence: parsing occurs first, followed by validation, regardless of the subclass. For the XML input, output reflects XML-specific parsing and validation messages, confirming two elements; for JSON, it verifies the presence of a required key and reports two keys. This demonstrates how the template method prevents subclasses from altering the overall flow while permitting data-format-specific logic. In Java, abstract classes are preferred over interfaces for the template method pattern because they enable the inclusion of concrete default methods (like the final processData()) alongside abstract ones, providing a partial implementation without requiring Java 8+ default interface methods. This aligns with the pattern's goal of defining an invariant algorithm skeleton in the superclass.

Python Implementation

In Python, the Template Method pattern leverages the abc module to define an abstract base class that outlines the algorithm's structure, permitting subclasses to customize primitive operations without altering the overall flow. This approach aligns with Python's dynamic typing, where method resolution occurs at runtime, enabling flexible extensions even if not all subclasses override every hook method. The core structure features an abstract base class Game with a concrete template method play() that invokes abstract methods initialize(), startPlay(), and endPlay(). Subclasses like Chess and Soccer provide concrete implementations for these methods, tailoring the game initialization, play logic, and conclusion to their respective rules.
python
from abc import ABC, abstractmethod

class Game(ABC):
    def play(self):
        """
        The template method defining the skeleton of the game algorithm.
        """
        self.initialize()
        self.startPlay()
        self.endPlay()

    @abstractmethod
    def initialize(self):
        """
        Abstract method for game initialization.
        """
        pass

    @abstractmethod
    def startPlay(self):
        """
        Abstract method for starting the game play.
        """
        pass

    @abstractmethod
    def endPlay(self):
        """
        Abstract method for ending the game.
        """
        pass

class Chess(Game):
    def initialize(self):
        print("Chess: Initializing the board with pieces.")

    def startPlay(self):
        print("Chess: [White](/page/White) moves first. Playing...")

    def endPlay(self):
        print("Chess: Game finished.")

class Soccer(Game):
    def initialize(self):
        print("Soccer: Setting up the field and teams.")

    def startPlay(self):
        print("Soccer: Kickoff! Playing...")

    def endPlay(self):
        print("Soccer: Match over.")

# Driver code to demonstrate execution
if __name__ == "__main__":
    chess = Chess()
    chess.play()

    print()  # Separator for readability

    soccer = Soccer()
    soccer.play()
When executed, the driver instantiates Chess and Soccer objects, invoking play() on each, which enforces the fixed sequence: initialization precedes play, followed by conclusion. This flow ensures the algorithm's invariance while demonstrating Python's runtime polymorphism, where missing overrides in subclasses would raise a TypeError only upon method call, rather than at instantiation. Python's implementation emphasizes conventions over enforced access modifiers, as there are no strict private or protected keywords; instead, single underscores signal internal use. This suits scripting scenarios, such as pipelines, where benefits from —objects need only implement required methods to participate, without formal checks. The shares conceptual similarities with the , as both enable variation in , but they differ fundamentally in their approach to flexibility. The template method relies on , allowing subclasses to override specific steps within a fixed algorithmic skeleton defined in a superclass, thereby promoting compile-time of algorithm parts. In contrast, the employs , encapsulating entire interchangeable as separate objects that can be dynamically selected and swapped at via to a context object. This distinction makes template method suitable for scenarios where the overall structure remains invariant, while strategy excels in environments requiring adaptability without subclass proliferation. Similarly, the template method pattern relates closely to the factory method pattern, where the latter serves as a focused on object creation rather than algorithmic flow. Both patterns defer certain responsibilities to subclasses, but factory method emphasizes the of objects through a dedicated method, often integrated as a step within a broader template method to handle creation logic flexibly. For instance, a template method might invoke a factory method to produce required components during its execution, combining algorithmic definition with polymorphic object generation. The template method pattern also intersects with the in managing behavioral variations, though their mechanisms diverge based on dynamism. Template method enforces a fixed sequence of steps through , ideal for stable outlines with predefined hooks for extension. Conversely, the uses to alter an object's behavior dynamically in response to internal state changes, encapsulating state-specific logic in separate classes that the delegates to at . This makes template method appropriate for invariant sequences, while state addresses evolving behaviors tied to contextual conditions. In complex systems, the template method pattern can be synergistically combined with the to achieve hybrid flexibility, where the template defines the high-level algorithm structure via inheritance, and strategies handle variable steps through runtime composition. This integration leverages the static robustness of template method with the dynamic interchangeability of strategy, enabling scalable designs in frameworks or applications requiring both fixed workflows and adaptable components.

Common Variations

One common variation of the template method pattern involves parameterizing the template method itself, allowing it to accept arguments that dynamically influence the algorithm's execution without necessitating new subclasses for every minor adjustment. These parameters can configure which steps are performed, alter the behavior of primitive operations, or select among optional hook methods, thereby enhancing flexibility in frameworks where runtime customization is essential. Another variation features multiple template methods within the same abstract , each defining a distinct algorithm skeleton while sharing underlying operations. This approach is particularly useful when an abstract must orchestrate several related but structurally different processes, enabling subclasses to override primitives as needed across the templates without code duplication. In scenarios where is constrained, such as in languages lacking support, a variation employs and to implement the . Here, the abstract delegates operations to composed objects rather than relying solely on subclass overrides, promoting and reducing to the inheritance hierarchy. To avoid issues from excessive subclassing leading to and deep inheritance hierarchies, practitioners limit the pattern's scope by carefully defining and methods, ensuring only essential variations are subclassed. This maintains and prevents fragile hierarchies in large-scale applications.

Benefits and Limitations

Advantages

The Template Method pattern promotes by centralizing the invariant portions of an algorithm within a single superclass method, thereby minimizing duplication when subclasses implement varying steps. This approach allows developers to define common logic once, such as shared preprocessing or postprocessing in workflows, and reuse it across multiple subclasses without repetition. It enhances flexibility by enabling subclasses to override only the primitive operations while preserving the overall algorithm structure, in line with the open-closed principle that favors extension over modification. For instance, in framework integration scenarios, this permits customization of specific behaviors without altering the core template, supporting scalable software evolution. The pattern ensures by enforcing a standardized skeleton of the algorithm across all implementations, which facilitates predictable outcomes and simplifies maintenance in collaborative team environments. This uniformity is particularly valuable in large codebases where multiple developers contribute to related components, reducing the risk of divergent implementations. Additionally, it improves by decomposing complex algorithms into a clear, high-level outline of steps, making the code's intent more accessible and aiding in the onboarding of new team members. The explicit separation of fixed and variable elements in the template method serves as a , allowing readers to quickly grasp the algorithm's flow without delving into intricate details.

Disadvantages

The template method pattern enforces a fixed sequence of steps in the algorithm's skeleton, which is defined within the base , thereby limiting flexibility when evolving requirements demand alterations to the overall structure or . This rigidity can increase the cost of modifications, as changes often require refactoring the base and potentially numerous subclasses, rather than allowing modular adjustments. Reliance on inheritance to extend the algorithm introduces dependencies that can result in rigid class hierarchies, where subclasses are tightly bound to the base class's implementation details. This approach may contravene the design of favoring , as it exposes subclasses to unintended ripple effects from superclass changes, complicating independent evolution of components. The pattern's inheritance-based structure also exacerbates the fragile base class problem, wherein alterations to the template method or primitive operations in the base class can inadvertently disrupt the behavior of subclasses, even if those subclasses do not override the affected elements. Misuse of hooks or optional methods further heightens this fragility, as unexpected overrides may alter the intended algorithm flow without clear indicators. In large-scale systems, repeated application of the template method can lead to deep chains, hindering by making , testing, and comprehension more challenging due to the interconnected nature of the hierarchy.

References

  1. [1]
    Template Method Design Pattern - SourceMaking
    The Template Method defines a skeleton of an algorithm in an operation, and defers some steps to subclasses.
  2. [2]
    Template Method - Refactoring.Guru
    Template Method is a behavioral design pattern that defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the ...Template Method in C · Template Method in Python · Template Method in Java
  3. [3]
    Discovering the Design Patterns You're Already Using in .NET
    This is an example of the Template Method pattern. The main algorithm skeleton is defined in a base class and subclasses can then plug in their own details ...
  4. [4]
    Design Patterns: Elements of Reusable Object-Oriented Software
    30-day returnsOct 31, 1994 · Design Patterns: Elements of Reusable Object-Oriented Software. By Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides; Published Oct 31, ...Missing: publisher | Show results with:publisher
  5. [5]
    [PDF] Template Method
    Page 1. Design Patterns: Elements of Reusable Object-Oriented Software. Template Method. Intent ... Application and Document subclasses vary those steps to suit ...
  6. [6]
  7. [7]
  8. [8]
    [PDF] Template Method design pattern and Public Inheritance
    [2] Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design patterns : elements of reusable object-oriented software. Addison-Wesley, Reading, Mass ...<|control11|><|separator|>
  9. [9]
    Template Method
    Template Method lets subclasses ... Template methods lead to an inverted control structure that's sometimes referred to as "the Hollywood principle ...
  10. [10]
    [PDF] Design Patterns Elements of Reusable Object-Oriented Software
    Erich Gamma/Richard Helm/Ralph Johnson/John Vlissides, Design Patterns: Elements of Reusable Object-. Oriented Software. Erich Gamma/Richard Helm/Ralph Johnson ...
  11. [11]
    HttpServlet (Java(TM) EE 7 Specification APIs)
    ### Summary of HttpServlet Service Method and Template Method Pattern
  12. [12]
    AbstractList (Java Platform SE 8 )
    ### Summary of AbstractList Implementation Using Template Method Pattern
  13. [13]
    [PDF] Interaction Programming-GUI-patterns Cristian Bogdan Sida 1
    • The AWT/Swing template method invokes also related classes, not just subclasses. • Template Method is at the core of frameworks. Frameworks. Coding with ...
  14. [14]
    Template Method Design Pattern - GeeksforGeeks
    Sep 13, 2025 · Template Method Design Pattern is a behavioral pattern that defines the skeleton of an algorithm in a base method while allowing subclasses ...
  15. [15]
    Template Method Pattern - Embedded Artistry
    Mar 24, 2022 · The Template Method pattern defines the skeleton of an algorithm or ... Design Patterns VS Design Principles: Template Method – Fluent C++.
  16. [16]
    Implementing the Template Method Pattern in Java | Baeldung
    Jan 11, 2024 · In this quick tutorial, we'll see how to leverage the template method pattern – one of the most popular GoF patterns.
  17. [17]
    Template Method in Java / Design Patterns - Refactoring.Guru
    Template Method is a behavioral design pattern that allows you to define a skeleton of an algorithm in a base class and let subclasses override the steps.Overriding Standard Steps Of... · Networks · Demo. Java: Client Code
  18. [18]
    Template Method in Python / Design Patterns - Refactoring.Guru
    Template Method is a behavioral design pattern that allows you to define a skeleton of an algorithm in a base class and let subclasses override the steps.
  19. [19]
    Strategy - Refactoring.Guru
    Template Method works at the class level, so it's static. Strategy works on the object level, letting you switch behaviors at runtime. State can be considered ...Strategy in Python · Strategy in C++ · Strategy in Go · Strategy in PHP
  20. [20]
    [PDF] The Template Method Pattern
    • “Hollywood principle”—don't ... See refactoring.guru/design-patterns/template-method/java/example. Template Method. GoF Class Behavioral. Template method.<|control11|><|separator|>
  21. [21]
    Example Design Patterns
    The State pattern makes the behavior of an object related to its state without polluting its operations with conditional statements. It gives you the appearance ...
  22. [22]
    [PDF] Josh Bloch Charlie Garrod
    – Template method pattern (next week). – Iterator pattern (next week) ... • Usually, favor composition/delegation over inheritance. – Inheritance violates ...
  23. [23]
    [PDF] Josh Bloch Charlie Garrod
    – Template method pattern. – Iterator pattern. – Decorator pattern. • Design ... Limitations of inheritance. • Suppose you want various extensions of a ...
  24. [24]
    [PDF] Holub on Patterns: Learning Design Patterns by Looking at Code
    ... Fragile-Base-Class Problem ... Template-Method pattern. In Template. Method, base-class code calls an overridable placeholder method, the real ...