Created
November 18, 2014 00:21
-
-
Save scottashipp/ec9f1c82fc7da24919cc to your computer and use it in GitHub Desktop.
Example of using static factory methods in Java
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 class Person { | |
static int NEXT_ID = 0; | |
private String first; | |
private String last; | |
private int id; | |
private Person() {} //prevents instantiation not using "create" | |
private Person(String f, String l, int i) { | |
first = f; | |
last = l; | |
id = i; | |
} | |
public static Person create(String f, String l) { | |
return new Person(f, l, NEXT_ID++); | |
} | |
public String getName() { | |
return String.format("%s %s", first, last); | |
} | |
public int getId() { | |
return id; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment