Created
September 7, 2011 10:57
-
-
Save bivas/1200281 to your computer and use it in GitHub Desktop.
Builder Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Foo { | |
Collection<Bar> getBars(); | |
Zoo getZoo(); | |
} | |
public interface Bar {} | |
public interface Zoo {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity(name = "Foo") | |
public final class FooDTO implements Foo { | |
@Id | |
@GeneratedValue | |
private Long id; | |
@NotEmpty | |
@OneToMany(targetEntity = BarDTO.class) | |
private Collection<Bar> bars = new HashSet<Bar>(); | |
@NotNull | |
@ManyToOne(targetEntity = ZooDTO.class) | |
private Zoo zoo; | |
FooDTO() {} | |
public Long getId() { | |
return id; | |
} | |
@Override | |
public Collection<Bar> getBars() { | |
return Collections.unmodifiableCollection(bars); | |
} | |
@Override | |
public Zoo getZoo() { | |
return zoo; | |
} | |
void setId(final Long id) { | |
this.id = id; | |
} | |
void setZoo(final Zoo zoo) { | |
this.zoo = zoo; | |
} | |
void setBars(final Collection<? extends Bar> bars) { | |
this.bars.clear(); | |
this.bars.addAll(bars); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class FooDTO implements Foo { | |
// ... described above | |
public static final Builder newBuilder() { | |
return Builder.create(); | |
} | |
public static final Builder newBuilder(Foo foo) { | |
return Builder.merge(foo); | |
} | |
public static final Builder updateBuilder(Foo foo) { | |
return Builder.update(foo); | |
} | |
public static final class Builder implements com.example.Builder<Foo> { | |
private FooDTO dto; | |
private static final Builder create() { | |
return new Builder(); | |
} | |
private static final Builder update(Foo foo) { | |
return new Builder(foo, false); | |
} | |
private static final Builder merge(Foo foo) { | |
return new Builder(foo, true); | |
} | |
private Builder() { | |
dto = new FooDTO(); | |
} | |
private Builder(Foo foo, boolean newInstance) { | |
this(); | |
dto.setId(newInstance ? null : foo.getId()); | |
dto.setZoo(foo.getZoo()); | |
dto.setBars(foo.getBars()); | |
} | |
public Builder setZoo(Zoo zoo) { | |
dto.setZoo(zoo); | |
return this; | |
} | |
public Builder addBar(Bar bar) { | |
dto.bars.add(bar); | |
return this; | |
} | |
public Builder removeBar(Bar bar) { | |
dto.bars.remove(bar); | |
return this; | |
} | |
public Builder addAllBars(Collection<? extends Bar> bars) { | |
dto.bars.addAll(bars); | |
return this; | |
} | |
public Builder removeAllBars(Collection<? extends Bar> bars) { | |
dto.bars.removeAll(bar); | |
return this; | |
} | |
public Builder removeBars() { | |
dto.bars.clear(); | |
return this; | |
} | |
@Override | |
public Foo build() { | |
return dto; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment