Post

Log inSign up

Post

Log inSign up

Nemanja Rogic on X: "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 algorithm without changing its structure. Introduction + demo code↓ #designpatterns #dotnet #csharp #coding #programming"

@rogic_nemanja
Nemanja Rogic
@rogic_nemanja
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 algorithm without changing its structure. Introduction + demo code↓ #designpatterns #dotnet #csharp #coding #programming
9:17 AM · Mar 28, 2021·
1

Log in or sign up for X

See what’s happening and join the conversation

Continue with phone
or
Log in with username or email

Relevant people

Avatar
Nemanja Rogic@rogic_nemanjaFollow
Senior Software Engineer @Microsoft • Passionate about software craftsmanship, shipping reliable products, and tackling challenging problems

Trending now

Terms·Privacy·Cookies·Accessibility·US TIDA·Ads Info·© 2026 X Corp.
  • @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    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 algorithm without changing its structure. Introduction + demo code↓ #designpatterns #dotnet #csharp #coding #programming
    9:17 AM · Mar 28, 2021·
    1
  • @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Mar 28, 2021
    We can demonstrate Template pattern on a conceptual example in order to see all possible extension points of the pattern. The first step is to define the AbstractClass, which defines the template method, and its concrete implementations.
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Mar 28, 2021
    The client code calls the template method to execute the algorithm. Client code does not have to know the concrete class of an object it works with, as long as it works with objects through the interface of their base class.
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Mar 28, 2021
    Most of the times, subclasses call methods from a superclass. However, when you use the Template design pattern, a template method (from the superclass ) calls methods from subclasses, this is known as Hollywood Principle – "don’t call us, we'll call you."
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Mar 28, 2021
    Use the Template Method pattern when you want to let clients extend only particular steps of an algorithm, but not the whole algorithm or its structure.
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Mar 28, 2021
    Use the pattern when you have several classes that contain almost identical algorithms with some minor differences. As a result, you might need to modify all classes when the algorithm changes.
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Mar 28, 2021
    + 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
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Mar 28, 2021
    - Some clients may be limited by the provided skeleton of an algorithm - Template methods tend to be harder to maintain the more steps they have
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Mar 28, 2021
    Full implementation of the previously shown example, as well as additional design pattern examples, can be found on github.com/nemanjarogic/D… For more details on design patterns theory check @RefactoringGuru
    GitHub - nemanjarogic/DesignPatternsLibrary: A comprehensive design patterns library implemented in...
    From github.com