Delegates: Difference between revisions
Content deleted Content added
No edit summary |
Added zkl |
||
Line 1,584: | Line 1,584: | ||
7 |
7 |
||
</pre> |
</pre> |
||
=={{header|zkl}}== |
|||
{{trans|Scala}} |
|||
<lang zkl>class Thingable{ var thing; } |
|||
class Delegator{ |
|||
var delegate; |
|||
fcn operation{ |
|||
if (delegate) delegate.thing; |
|||
else "default implementation" |
|||
} |
|||
} |
|||
class Delegate(Thingable){ thing = "delegate implementation" }</lang> |
|||
<lang zkl> // Without a delegate: |
|||
a:= Delegator(); |
|||
a.operation().println(); //--> "default implementation" |
|||
// With a delegate: |
|||
a.delegate = Delegate(); |
|||
a.operation().println(); //-->"delegate implementation"</lang> |
|||
A second example |
|||
<lang zkl>class [static] Logger{ // Only one logging resource |
|||
var [mixin=File] dst; // File like semantics, eg Data, Pipe |
|||
dst = File.DevNull; |
|||
// initially, the logger does nothing |
|||
fcn log(msg){dst.writeln(vm.pasteArgs())} |
|||
}</lang> |
|||
<lang zkl>Logger.log("this is a test"); //-->nada |
|||
Logger.dst=Console; |
|||
Logger.log("this is a test 2"); //-->writes to Console |
|||
class B(Logger){ log("Hello from ",self,"'s constructor"); } |
|||
B(); //-->Hello from Class(B)'s constructor</lang> |
|||
The base class B was constructed at startup, |
|||
so the first Hello went to DevNull as all base classes are created before |
|||
code runs (base classes are used to create class instances, eg B()). |
|||
{{Omit From|ALGOL 68}} <!-- it isn't immediately obvious that ALGOL 68 is object oriented --> |
{{Omit From|ALGOL 68}} <!-- it isn't immediately obvious that ALGOL 68 is object oriented --> |