Menu
CoddyTech

Composition and Delegation

Lesson 38 of 48 in Coddy's Luau for C# Developers course.

C# composition can retain a class collaborator or copy a struct whose reference fields may still be shared. Luau table fields retain references. Delegate through the supplied policy and decide explicitly whether the contract calls for shared behavior or copied independent state.

C# comparison

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
var order=new Order(new DoublePolicy()); Console.WriteLine(order.Total());
interface IPolicy {int Apply(int n);}
class DoublePolicy:IPolicy {public int Apply(int n)=>n*2;}
class Order {private IPolicy policy; public Order(IPolicy p){policy=p;} public int Total()=>policy.Apply(3);}

Luau behavior

Composition stores a collaborator and delegates through its public protocol. A structural type can describe that protocol, but observable delegation is still the contract. The locked driver injects alternate policies, so hard-coding one policy in the app cannot satisfy the object API.

local policy={}
function policy:apply(n) return n*2 end
local order={policy=policy}
function order:total() return self.policy:apply(3) end
print(order:total())

Expected Luau output:

6
challenge icon

Challenge

Easy

Implement a pricing policy in policy.luau with apply(n) multiplying n by a stored factor. Model stores the policy and exposes total(n). Input is factor and quantity. Return the delegated total.

Edit app.luau, model.luau, policy.luau. The visible locked main.luau reads stdin into a sequence of lines, calls app.run(lines), and prints its returned value with one final newline. Do not print diagnostics or change the module names. Empty input gives an empty sequence. All numeric inputs are small enough to avoid overflow.

Public API checks: Policy.new(factor) provides apply(n). Model.new(policy) must delegate total(n) to the supplied policy, including a custom object implementing apply, and retain that collaborator independently. The locked driver checks this behavior before calling app.run. These checks print nothing when successful.

Try it yourself

-- Public API checks
do
local Model=require("./model")
local Policy=require("./policy")
local p=Policy.new(3)
assert(p:apply(4)==12 and p:apply(0)==0,"policy multiplies by its factor")
local a=Model.new(p)
assert(a:total(2)==6,"model must use its pricing policy")
local calls=0
local custom={apply=function(self,n) calls=calls+1;return n+7 end}
local b=Model.new(custom)
assert(b:total(5)==12 and calls==1,"model must delegate to the supplied collaborator")
assert(a:total(1)==3,"models retain their own policy")
end
local app = require("./app")
local lines = {}
for line in io.lines() do lines[#lines + 1] = line end
print(app.run(lines))
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Luau for C# Developers

Practice on your own: Online Luau compiler