Sitemap
Mehmet Basrioglu

Mehmet Basrioglu

Software Engineer | AWS, Java, Spring Boot, React, Kafka, Postgres, Mongo. Scalable systems. Support & Books: shopier.com/mehmetbasriogludanismanlik

Behavioral Pattern: Template Method

2 min readOct 11, 2025

--

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.

Which scenarios need the Template Method?

  • Reusable Abstract Classes
  • File Data Operation
  • Reporting or Document Generation
  • Authentication and Authorization Flows
  • Payment Processing Workflows
  • Messaging or Notification Systems
  • External API Request from Spring to Any

Get Mehmet Basrioglu’s stories in your inbox

Join Medium for free to get updates from this writer.

SOLID Friendly Example

package com.arabamburada.backend.core.services;

public interface TokenExtractor {
String extractSubject(String token);
}

package com.arabamburada.backend.core.services;

public interface TokenGenerator {
String generateToken(String subject);
}
package com.arabamburada.backend.core.services;

public interface TokenService extends
TokenGenerator, TokenValidator, TokenExtractor {
}
package com.arabamburada.backend.core.services;

public interface TokenValidator {
boolean validateToken(String token);
}
package com.arabamburada.backend.core.services.impl;

import com.arabamburada.backend.core.services.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public abstract class BaseTokenServiceImpl implements TokenService {

@Override
public final String extractSubject(String token) {
if (!validateToken(token)) {
throw new IllegalArgumentException("Invalid token");
}
return doExtractSubject(token);
}

protected abstract String doExtractSubject(String token);

// Common logic
@Override
public final String generateToken(String subject) {
validateSubject(subject);
logTokenGeneration(subject);
return doGenerateToken(subject);
}

@Override
public final boolean validateToken(String token) {
if (token == null || token.isEmpty()) {
return false;
}
log.info("Base validate token invoked");
return doValidateToken(token);
}

// Hook methods - subclasses will implement
protected abstract String doGenerateToken(String subject);
protected abstract boolean doValidateToken(String token);

// Common helper methods
protected void validateSubject(String subject) {
if (subject == null || subject.isEmpty()) {
throw new IllegalArgumentException("Subject cannot be null");
}
}

protected void logTokenGeneration(String subject) {
System.out.println("Generating token for: " + subject);
}
}
package com.arabamburada.backend.core.services.impl;

import com.arabamburada.backend.core.services.JwtTokenService;
import org.springframework.stereotype.Service;

@Service
public class JwtTokenServiceImpl extends BaseTokenServiceImpl implements JwtTokenService {

@Override
protected String doExtractSubject(String token) {
return token.replace("jwt-token-for-", "");
}

@Override
protected String doGenerateToken(String subject) {
return "jwt-token-for-" + subject;
}

@Override
protected boolean doValidateToken(String token) {
return token.startsWith("jwt-token");
}


}

Conclusion

The Template Method behavioral pattern is a best practice for designing reusable and extendable infrastructures. By defining the skeleton of an algorithm in a base class and allowing subclasses to implement specific steps, it promotes code reuse, consistency, and separation of concerns. This approach is particularly effective for common workflows like token generation and validation in a security infrastructure.

--

--

Mehmet Basrioglu
Mehmet Basrioglu

Written by Mehmet Basrioglu

Software Engineer | AWS, Java, Spring Boot, React, Kafka, Postgres, Mongo. Scalable systems. Support & Books: shopier.com/mehmetbasriogludanismanlik