new tasks
This commit is contained in:
parent
2a4d27cea0
commit
80737d5a6a
1194 changed files with 15353 additions and 1 deletions
43
Task/Delegates/Java/delegates.java
Normal file
43
Task/Delegates/Java/delegates.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