Created
March 23, 2020 10:25
-
-
Save Guseyn/9e633a2b3d265e0dd9b8fca773afb0a7 to your computer and use it in GitHub Desktop.
Declarative approach via interface with getters
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 User { | |
String name(); | |
int age(); | |
} | |
class SimpleUser implements User { | |
private String name; | |
private int age; | |
//...constructor | |
String name () { | |
return this.name; | |
} | |
int age() { | |
return this.age; | |
} | |
} | |
class JSONUser { | |
private User; | |
// constructor... | |
public void value() { | |
return String.format("{ \"name\":\"%s\", \"age\":\"%d\" }", this.user.name(), this.user.age()) | |
} | |
} | |
new JSONUser( | |
new SimpleUser('John', 26) | |
).value() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment