Delegates: Difference between revisions

Content deleted Content added
Added an Algol 68 implementation
Line 142: Line 142:


</lang>
</lang>

=={{header|Algol 68}}==
As Algol 68 doesn't have classes, we supply a non-OO approximation, similar to the C version.
<lang algol68># An Algol 68 approximation of delegates #

# The delegate mode - the delegate is a STRUCT with a single field #
# that is a REF PROC STRING. If this is NIL, it doesn't implement #
# thing #
MODE DELEGATE = STRUCT( REF PROC STRING thing );


# A delegator mode that will invoke the delegate's thing method #
# - if there is a delegate and the delegate has a thing method #
MODE DELEGATOR = STRUCT( REF DELEGATE delegate
, PROC( REF DELEGATE )STRING thing
);

# constructs a new DELEGATE with the specified PROC as its thing #
# Algol 68 HEAP is like "new" in e.g. Java, but it can't take #
# parameters, so this PROC does the equivalent #
PROC new delegate = ( REF PROC STRING thing )REF DELEGATE:
BEGIN
REF DELEGATE result = HEAP DELEGATE;
thing OF result := thing;

result
END # new delegate #
;

# constructs a new DELEGATOR with the specified DELEGATE #
PROC new delegator = ( REF DELEGATE delegate )REF DELEGATOR:
HEAP DELEGATOR := ( delegate
, # anonymous PROC to invoke the delegate's thing #
( REF DELEGATE delegate )STRING:
IF delegate IS REF DELEGATE(NIL)
THEN
# we have no delegate #
"default implementation"

ELIF thing OF delegate IS REF PROC STRING(NIL)
THEN
# the delegate doesn't have an implementation #
"default implementation"

ELSE
# the delegate can thing #
thing OF delegate

FI
)
;


# invokes the delegate's thing via the delagator #
# Because the PROCs of a STRUCT don't have an equivalent of e.g. Java's #
# "this", we have to explicitly pass the delegate as a parameter #
PROC invoke thing = ( REF DELEGATOR delegator )STRING:
# the following is Algol 68 for what would be written in Java as #
# "delegator.thing( delegator.delegate )" #
( thing OF delegator )( delegate OF delegator )
;

main:
(

print( ( "No delegate : "
, invoke thing( new delegator( NIL ) )
, newline
, "Delegate with no thing: "
, invoke thing( new delegator( new delegate( NIL ) ) )
, newline
, "Delegate with a thing : "
, invoke thing( new delegator( new delegate( HEAP PROC STRING := STRING: ( "delegate implementation" ) ) ) )
, newline
)
)

)
</lang>
{{out}}<pre>
No delegate : default implementation
Delegate with no thing: default implementation
Delegate with a thing : delegate implementation
</pre>


=={{header|C}}==
=={{header|C}}==
Line 1,858: Line 1,942:




{{Omit From|ALGOL 68}} <!-- it isn't immediately obvious that ALGOL 68 is object oriented -->
<!-- Omit From|ALGOL 68 although it isn't immediately obvious that ALGOL 68 is object oriented, if C can do it... -->
{{omit From|Fortran}}
{{omit From|Fortran}}
{{omit From|Maxima}}
{{omit From|Maxima}}