Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
43
Task/Delegates/Java/delegates-1.java
Normal file
43
Task/Delegates/Java/delegates-1.java
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
interface Thingable {
|
||||
String thing();
|
||||
}
|
||||
|
||||
class Delegator {
|
||||
public Thingable delegate;
|
||||
|
||||
public String operation() {
|
||||
if (delegate == null)
|
||||
return "default implementation";
|
||||
else
|
||||
return delegate.thing();
|
||||
}
|
||||
}
|
||||
|
||||
class Delegate implements Thingable {
|
||||
public String thing() {
|
||||
return "delegate implementation";
|
||||
}
|
||||
}
|
||||
|
||||
// Example usage
|
||||
// Memory management ignored for simplification
|
||||
public class DelegateExample {
|
||||
public static void main(String[] args) {
|
||||
// Without a delegate:
|
||||
Delegator a = new Delegator();
|
||||
assert a.operation().equals("default implementation");
|
||||
|
||||
// With a delegate:
|
||||
Delegate d = new Delegate();
|
||||
a.delegate = d;
|
||||
assert a.operation().equals("delegate implementation");
|
||||
|
||||
// Same as the above, but with an anonymous class:
|
||||
a.delegate = new Thingable() {
|
||||
public String thing() {
|
||||
return "anonymous delegate implementation";
|
||||
}
|
||||
};
|
||||
assert a.operation().equals("anonymous delegate implementation");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue