Skip to content

Instantly share code, notes, and snippets.

@Guseyn
Created March 23, 2020 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Guseyn/9e633a2b3d265e0dd9b8fca773afb0a7 to your computer and use it in GitHub Desktop.
Save Guseyn/9e633a2b3d265e0dd9b8fca773afb0a7 to your computer and use it in GitHub Desktop.
Declarative approach via interface with getters
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