Skip to content

Instantly share code, notes, and snippets.

@sergiolopes
Created October 19, 2009 21:32
Show Gist options
  • Save sergiolopes/213728 to your computer and use it in GitHub Desktop.
Save sergiolopes/213728 to your computer and use it in GitHub Desktop.
exemplo de classe imutável usando flyweight
import java.util.Calendar;
public final class Periodo {
private final Calendar inicio;
private final Calendar fim;
public Periodo(Calendar inicio, Calendar fim) {
this((Calendar) inicio.clone(), (Calendar) fim.clone(), false);
}
private Periodo(Calendar inicio, Calendar fim, boolean nada) {
this.inicio = inicio;
this.fim = fim;
}
public Calendar getInicio() {
// copia defensiva
return (Calendar) inicio.clone();
}
public Calendar getFim() {
// copia defensiva
return (Calendar) fim.clone();
}
public Periodo adiaUmaSemana() {
Calendar novoFim = (Calendar) this.fim.clone();
novoFim.add(Calendar.DAY_OF_MONTH, 7);
return new Periodo(this.inicio, novoFim, true); // flyweight
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment