Last active
March 23, 2020 10:24
-
-
Save Guseyn/ba8190fad8a85345deb96232ad4957c9 to your computer and use it in GitHub Desktop.
Declarative approach via 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
class User { | |
private String name; | |
private int age; | |
// ... constructor | |
public String name() { | |
return this.name; | |
} | |
public 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 User('John', 26) | |
).value() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment