/ Published in: Java

An immutable (a new object is created for each change) Java class builder pattern example.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
public class RadioBuilder { private int buttons; private CDPlayer cdPlayer; private MP3Player mp3Player; public static RadioBuilder create() { return new RadioBuilder(4, CDPlayerBuilder.create().build(), MP3PlayerBuilder.create().build()); } public RadioBuilder(int buttons, CDPlayer cdPlayer, MP3Player mp3Player) { this.buttons = buttons; this.cdPlayer = cdPlayer; this.mp3Player = mp3Player; } public RadioBuilder withButtons(int buttons) { return new RadioBuilder(buttons, cdPlayer, mp3Player); } public RadioBuilder with(CDPlayer cdPlayer) { return new RadioBuilder(buttons, cdPlayer, mp3Player); } public RadioBuilder with(MP3Player mp3Player) { return new RadioBuilder(buttons, cdPlayer, mp3Player); } public Radio build() { return new Radio(buttons, cdPlayer, mp3Player); } }
Comments
