Menu
CoddyTech

Delegate to a Collaborator

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

Composition lets an order ask a separate pricing policy to calculate a total. A C design can store a pointer to the policy and a function pointer describing its operation. Lua stores a reference to the collaborator table and calls its method. The order does not need to repeat the pricing formula. It only preserves the collaborator and forwards the quantity. The examples use a factor of two, but another policy can implement the same operation with a different rule.

C

#include <stdio.h>
struct Policy {
    int factor;
    int (*apply)(const struct Policy *self, int quantity);
};
struct Order { const struct Policy *policy; };
static int multiply(const struct Policy *self, int quantity)
{
    return self->factor * quantity;
}
static int order_total(const struct Order *self, int quantity)
{
    return self->policy->apply(self->policy, quantity);
}
int main(void)
{
    struct Policy policy = {2, multiply};
    struct Order order = {&policy};
    printf("%d\n", order_total(&order, 3));
    return 0;
}

Lua

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

Porting trap

Storing a collaborator does not copy it. C must keep the pointed-to policy alive for every use; Lua keeps a referenced table reachable automatically. In both examples, modifying shared policy state would affect every order using that policy.

challenge icon

Challenge

Easy

Implement a pricing policy in policy.lua 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.lua, model.lua, policy.lua. The visible locked main.lua 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 Lua for C Developers

Practice on your own: Online Lua compiler