Last active
May 24, 2017 15:21
-
-
Save westonal/a41818b839bed76f7fa90dd90c43ae6a to your computer and use it in GitHub Desktop.
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
| class SomeThing { | |
| final Dependency1 dependency1; | |
| Dependency2 dependency2; | |
| Something(Dependency1 dependency1){ | |
| this.dependency1 = dependency1; | |
| } | |
| void setDependency2(Dependency2 dependency2){ | |
| this.dependency2 = dependency2; | |
| } | |
| void somethingThatNeedsBoth(){ | |
| dependency1.doIt(); | |
| dependency2.doIt(); //may not be set, order of calls is important: bad OO | |
| } | |
| } | |
| // Instead setting second dependency returns another object | |
| class SomeThing { | |
| final Dependency1 dependency1; | |
| Something(Dependency1 dependency1){ | |
| this.dependency1 = dependency1; | |
| } | |
| SomeThing2 setDependency2(Dependency2 dependency2){ | |
| return new SomeThing2(dependency2); | |
| } | |
| class SomeThing2 { | |
| final Dependency2 dependency2; | |
| SomeThing2(Dependency2 dependency2){ | |
| this.dependency2 = dependency2; | |
| } | |
| void somethingThatNeedsBoth(){ | |
| dependency1.doIt(); | |
| dependency2.doIt(); //both must have be set | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment