40 lines
869 B
Text
40 lines
869 B
Text
class thingable
|
|
function thing() end
|
|
end
|
|
|
|
-- Delegate that doesn't implement thingable.
|
|
class delegate
|
|
function __construct() end
|
|
end
|
|
|
|
-- Delegate that implements thingable.
|
|
class delegate2 extends thingable
|
|
function __construct() end
|
|
|
|
function thing() return "delegate implementation" end
|
|
end
|
|
|
|
class delegator
|
|
function __construct()
|
|
self.delegate = nil
|
|
end
|
|
|
|
function operation()
|
|
if !self.delegate or !(self.delegate instanceof thingable) then
|
|
return "default implementation"
|
|
end
|
|
return self.delegate:thing()
|
|
end
|
|
end
|
|
|
|
-- Without a delegate.
|
|
local d = new delegator()
|
|
print(d:operation())
|
|
|
|
-- With a delegate that doesn't implement thingable.
|
|
d.delegate = new delegate()
|
|
print(d:operation())
|
|
|
|
-- With a delegate that does implement thingable.
|
|
d.delegate = new delegate2()
|
|
print(d:operation())
|