O'Reilly logo

Quality Code: Software Testing Principles, Practices, and Patterns by Stephen Vance

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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)) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required