Created
May 14, 2020 03:47
-
-
Save ahojukka5/c35f76f51b791b4862fa211a41170715 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
public class Main3 { | |
private static class Data1 { | |
private int data = 5; | |
public int getData() { | |
return data; | |
} | |
} | |
private static class Data2 { | |
private int data = 10; | |
public int acquireData() { | |
return data; | |
} | |
} | |
private interface DataInterface<T> { | |
public int getData(T data); | |
} | |
private static class Manipulator<T> { | |
private DataInterface<T> iface; | |
private T data; | |
public void setInterface(DataInterface<T> iface) { | |
this.iface = iface; | |
} | |
public Manipulator(T data) { | |
this.data = data; | |
} | |
public void manipulate() { | |
System.out.println(iface.getData(data)); | |
} | |
} | |
public static void main(String[] args) { | |
Data1 D1 = new Data1(); | |
Data2 D2 = new Data2(); | |
Manipulator<Data1> M1 = new Manipulator<>(D1); | |
M1.setInterface((data) -> data.getData()); | |
Manipulator<Data2> M2 = new Manipulator<>(D2); | |
M2.setInterface((data) -> data.acquireData()); | |
M1.manipulate(); | |
M2.manipulate(); | |
// DataInterface<Data1> if1 = (data) -> data.getData(); | |
// DataInterface<Data2> if2 = (data) -> data.acquireData(); | |
// System.out.println(if1.getData(D1)); | |
// System.out.println(if2.getData(D2)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment