How is composition achieved?

What is the best method and what do u guys do?

1) Give the object keys that refrence to classes its composed of:

   local NewCar = setmetatable(properties, {__index = Car})
   NewCar.Wheels = Wheels.New()
   NewCar.Engine =
   return NewCar

2) Make the class composed of components:

local Components = {Classes}
local Car = setmetatable({}, {__index = function(t, k)
   local m 
   for _, Component in Components do
       if Component[k] then 
           m = Component[k]
   return m
  • Option 1
  • Option 2

0 voters

1 Like

I personally use Option 1.

Option 2 doesn’t look appealing to the eye (at least for me). I’d rather take Option 1.

Thinking about it in hindsight, the second option seems more like multiple inheritance as the methods of the components are being set as the methods for the object, where the first option is moreso composition as u are creating a whole new component key in the object

Second option is literally just multiple inheritance, sorry if i confused anyone.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.