Delegation VS Fowarding

Moderatoren: pmueller, SE - Design and Construction

Benutzeravatar
AizazZaidee
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 106
Registriert: 20. Apr 2016 22:49

Delegation VS Fowarding

Beitrag von AizazZaidee » 1. Jul 2018 13:01

Hi,
Is the following solution is correct with respect to Forwarding and Delegation?

Q: Implement forwarding and delegation semantics in the method m() in the respective classes.

Code: Alles auswählen

trait Base {
    def m();
    def f();
}

class Callee extends Base{
    override def m(): Unit = {
        m(this)
    }

    override def f(): Unit = {}

    def m(self: Base): Unit ={
        self.f()
    }
}

class OtherClassForwarding extends Base {
    var callee = new Callee

    override def m(): Unit = {
        //Forwarding
        super.m()
    }

    override def f(): Unit = {}
}

class otherClassDelegation extends Base{
    var callee = new Callee

    override def m(): Unit = {
        //Delegation
        callee.m(this)
    }

    override def f(): Unit = {}
}

eichberg
Moderator
Moderator
Beiträge: 448
Registriert: 25. Sep 2007 12:12
Kontaktdaten:

Re: Delegation VS Fowarding

Beitrag von eichberg » 2. Jul 2018 12:49

Hi,it goes in the right direction. But, you are only implementing the required infrastructure for m. Furthermore, the override in OtherClassForwarding is useless.

topracer
Mausschubser
Mausschubser
Beiträge: 53
Registriert: 10. Jan 2014 19:14

Re: Delegation VS Fowarding

Beitrag von topracer » 14. Jul 2018 00:46

I guess that for forwarding semantics it is

Code: Alles auswählen

class OtherClassForwarding extends Base {
	...
	def m() {
		callee.m()
	}
}
because the object tells callee just to do something and callee does it for itself, without caring about the OtherClassForwarding object anymore.

And for delegation

Code: Alles auswählen

class OtherClassDelegation extends Base {
	...
	def m() {
		callee.m(this)
	}
}
because the object orders callee to do something (but not without attaching a "backup reference" to itself) which in return sends a "receipt" (or whatever) back to the OtherClassForwarding object.

Is that correct?

Antworten

Zurück zu „Software Engineering - Design and Construction“