Last active
October 12, 2015 10:18
-
-
Save orhanobut/7a37bbcdbb2f30bbd872 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
interface Persistence { | |
void delete(); | |
void add(String value); | |
String get(); | |
} | |
static class EmptyPersistence implements Persistence { | |
private static Persistence persistence; | |
static Persistence getInstance() { | |
if (persistence == null) { | |
persistence = new EmptyPersistence(); | |
} | |
return persistence; | |
} | |
private EmptyPersistence(){ | |
// control the instance | |
} | |
@Override void delete(){ | |
} | |
@Override void add(String value) { | |
} | |
@Override String get() { | |
return null; | |
} | |
} | |
class FooBar { | |
private Persistence persistence = EmptyPersistence.getInstance(); | |
void persistence(Persistence persistence) { | |
this.persistence = persistence; | |
} | |
private void delete() { | |
... | |
persistence.delete(); | |
} | |
private String get() { | |
return persistence.get(); | |
} | |
private void save(String value) { | |
persistence.save(value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment