Last active
February 10, 2017 21:42
-
-
Save genderquery/cd4071fd15e59044948e4f006e482114 to your computer and use it in GitHub Desktop.
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
package com.example; | |
public abstract class AbstractBuilder<T> { | |
protected T mObject; | |
public AbstractBuilder() { | |
// Type parameter 'T' cannot be instantiated directly | |
mObject = new T(); | |
} | |
public AbstractBuilder(T object) throws IllegalArgumentException { | |
if (object == null) { | |
throw new IllegalArgumentException("'object' cannot be null"); | |
} | |
mObject = object; | |
} | |
public abstract T build(); | |
} |
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
package com.example; | |
import java.util.Date; | |
public final class Person { | |
private String mName; | |
private Date mDateOfBirth; | |
private String mGender; | |
Person() {} | |
public String getName() { | |
return mName; | |
} | |
public Date getDateOfBirth() { | |
return mDateOfBirth; | |
} | |
public String getGender() { | |
return mGender; | |
} | |
public final static class Builder extends AbstractBuilder<Person> { | |
public Builder() { | |
super(); | |
} | |
public Builder(final Person object) throws IllegalArgumentException { | |
super(object); | |
} | |
@Override | |
public Person build() { | |
return mObject; | |
} | |
public Builder name(final String name) { | |
mObject.mName = name; | |
return this; | |
} | |
public Builder dateOfBirth(final Date dateOfBirth) { | |
mObject.mDateOfBirth = dateOfBirth; | |
return this; | |
} | |
public Builder gender(final String gender) { | |
mObject.mGender = gender; | |
return this; | |
} | |
} | |
} |
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
package com.example; | |
import java.util.Calendar; | |
import java.util.GregorianCalendar; | |
public class Usage { | |
void usage() { | |
final Person avery = new Person.Builder() | |
.name("Avery") | |
.dateOfBirth(new GregorianCalendar(1987, Calendar.SEPTEMBER, 24).getTime()) | |
.gender(null) | |
.build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment