Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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