Last active
December 23, 2015 02:35
-
-
Save jburwell/f5162ad2d2de32c842b3 to your computer and use it in GitHub Desktop.
Null Object Pattern Example
This file contains hidden or 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 Account extends Nullable { | |
| final static NULL = new NullAccount(); | |
| String getName(); | |
| String getDescription(); | |
| // Whether or not this account can be used to conduct business | |
| boolean isActive(); | |
| // Whether or not the internal state of this account is valid | |
| boolean isValid(); | |
| private final class NullAccount implements Account { | |
| private NullAccount() { | |
| super(); | |
| } | |
| @Override | |
| public boolean isNull() { | |
| return true; | |
| } | |
| @Override | |
| public String getName() { | |
| return ""; | |
| } | |
| @Override | |
| public Sting getDescription() { | |
| return ""; | |
| } | |
| @Override | |
| public boolean isActive() { | |
| return false; | |
| } | |
| @Override | |
| public boolean isValid() { | |
| return true; | |
| } | |
| @Override | |
| public String toString() { | |
| return "Null Account"; | |
| } | |
| } | |
| } |
This file contains hidden or 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 Nullable { | |
| boolean isNull(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment