ID:1836609
 
(See the best response by Zecronious.)
What's the equivalent of Composition in BYOND?

In C++ they have something called Composition over inheritance.

How could I achieve this effect with Dream Maker.
Best response
This is the way I like to do it.

mob
Login()
var
Alarmclock/myclock = new/Alarmclock()
Timebomb/mybomb = new/Timebomb()

myclock.setMaxTime(200)
myclock.start()

datum
proc
event(EVENT)

Alarmclock
var
Timer/timer = null

New()
timer = new/Timer(src)

proc
setMaxTime(TIME)
timer.setMaxTime(TIME)

start()
timer.start()

stop()
timer.stop()

event(EVENT)
switch(EVENT)
if("timer finished")
world << "BEEP BEEP"

Timebomb
var
Timer/timer = null

New()
timer = new/Timer(src)
timer.maxTime = 100
timer.start()

event(EVENT)
if("timer finished")
world << "BOOOOOOOM"

Timer
var
datum/parent = null
time = 0
maxTime = 0
paused = 1

New(PARENT)
parent = PARENT

proc
setTime(MILLISECONDS)
time = MILLISECONDS

setMaxTime(MAXTIME)
maxTime = MAXTIME

start()
paused = 0
runTimer()

stop()
paused = 1

runTimer()
spawn()
while(time < maxTime && !paused)
time ++
sleep(1)
if(time >= maxTime)
parent.event("timer finished")
I should probably combine what you have with setting variables to value of a return of a function for more complete effect. Thanks. Any other suggestions welcome.

Dantum types + return values.

Taking datum types to represent static functionality is what you're suggesting. #Subbing in.
Taking datum types to represent static functionality is what you're suggesting.

Yep, that's a very neat way of putting it.

I should probably combine what you have with setting variables to value of a return of a function for more complete effect.

Can you show me how you did that when/if you do please? I would be interested to see it.
Sir Quizalot:
Any other suggestions welcome.

Composition is pretty much a datum containing other datums. However DM doesn't support interfaces (only inheritance).

A crude example:
object_a
var/object_b/obj_b
var/object_c/obj_c

New()
..()
obj_b = new/object_b()
obj_c = new/object_c()

proc/name()
return obj_b.name
proc/desc()
return obj_c.desc

object_b
var/name = "Some Name"
object_c
var/desc = "Some Desc"

client/New()
..()
var/object_a/o = new()
src << o.name()
src << o.desc()
In response to Mr_Goober
You want that to be object1 being composed of an object2 and an object3 though right? (alongside calling the procs within obj1 for obj2 and obj3)
I'm guessing that was a mistake?
In response to Turboskill
Turboskill wrote:
You want that to be object1 being composed of an object2 and an object3 though right? (alongside calling the procs within obj1 for obj2 and obj3)
I'm guessing that was a mistake?

Yeah woops. Fixed.
GatewayRa wrote:

True compositions can't be done in BYOND (well, except by inheritance, but I don't know how that memory is managed in BYOND).

True Composition-

Composition is most importantly a concept. We can achieve the spirit of any concept in any programming language.

Composition- One object can be built of another object. Has ownership.
Example video- http://www.lynda.com/Java-tutorials/ Using-aggregation-composition/96949/106092-4.html

I really think the best bet is setting variables to function returns. In that sense it may not be inheritance per say.

With all of your input I'll figure it out. So thanks.

I'm curious if you had to achieve that effect how would you do it Ra besides inheritance?

Thanks

GatewayRa wrote:
He said composition over inheritance, not composition.

Actually, he asked about both in the OP.
GatewayRa wrote:
Flick wrote:
GatewayRa wrote:
He said composition over inheritance, not composition.

Actually, he asked about both in the OP.

Yeah, and? They didn't even give him composition; they gave aggregation. You can conceptualize things however way you want, but it doesn't mean it's accurate.

The codes others provided didn't even fit the description of what a composition is. They didn't provide assurance that the objects are deleted when the parent is; they're relying on weak references (meaning if you reference the variable, it won't get deleted when the parent does). It's invalid and wrong.

Edit:
Here you go: http://stackoverflow.com/questions/885937/ difference-between-association-aggregation-and-composition

I'm not really a programmer. I didn't claim that they gave him correct information, I wouldn't know. All I know about any of it is what I read in the last few hours for my own curiosity, which is why I didn't respond to his questions. Just pointing out that he did actually ask about both, since you stated that he didn't.

I'll give that link a read when I get home. Thanks.
IMO "composition over inheritance" is bad advice--not because it never applies, but because it's as terrible a blanket rule as never using goto. Any organizational method has its strong and weak points, and the key to being a good programmer is predicting which of those points will be most relevant to the project. Composition has strong advantages, but it also has strong disadvantages.

In a language like DM, I don't see that composition's maintenance advantages would be high. The only major purpose to it would be to achieve what single-inheritance cannot.

That stackoverflow link is very good though. Definitely worth a quick read.