Encapsulate and Override

A common operation in authentication, cache maintenance, and several other concerns is to expire an object that is older than some threshold. Listing 6-12 shows a typical implementation for an authentication token with a two-week lifetime.

Listing 6-12: A typical implementation to expire an authentication token with a two-week lifetime

public class AuthenticationManager {  public static final EXPIRATION_DAYS = 14;  ...  // Return indicates if the token was expired  public boolean expire(AuthenticationToken token) {    Calendar expirationHorizon = Calendar.getInstance();    expirationHorizon.add(Calendar.DAY, -EXPIRATION_DAYS);    Calendar createdDate = token.getCreatedDate();    if (expirationHorizon.after(createdDate)) ...

Get Quality Code: Software Testing Principles, Practices, and Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.