Java 8 Mixins VS Scala - What's the difference?

Moderatoren: pmueller, SE - Design and Construction

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

Java 8 Mixins VS Scala - What's the difference?

Beitrag von AizazZaidee » 3. Jul 2018 11:09

Using Java 8 we can also use Delegation, Anyone can help me out with pointing out the difference between Scala Mixins and Java Mixins?

Code: Alles auswählen

public interface Airport {

    // To be implemented in subclass
    List < Aircraft > getAircrafts();

    default void land(Aircraft aircraft) {
        getAircrafts().add(aircraft);
    }

    default void depart(Aircraft aircraft) {
        getAircrafts.remove(aircraft);
    }
}


public interface Ship {
    // To be implemented in subclass
    List < Cargo > getCargos();

    default void addCargo(Cargo c) {
        getCargos().add(c);
    }
    void removeCargo(Cargo c) {
        getCargos().remove(c);
    }
}


class AircraftCarrier implements Ship, Airport {

    List < Aircraft > aircrafts = new ArrayList < > ();
    List < Cargo > cargoes = new ArrayList < > ();

    @Override
    public List < Aircraft > getAircrafts() {
        return aircrafts;
    }

    @Override
    public List < Cargo > getCargoes() {
        return cargoes;
    }
}
}

AircraftCarrier carrier = new AircraftCarrier();
carrier.addCargo(c);
carrier.land(a);
[I wrote this code here so maybe there are some problems with this code like semicolon... but conceptually it should compile]

Thanks,

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

Re: Java 8 Mixins VS Scala - What's the difference?

Beitrag von eichberg » 3. Jul 2018 11:37

I'm sorry, but I can't find any "delegation semantics" in the shown code. However, you can basically use the same strategy in both cases.

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

Re: Java 8 Mixins VS Scala - What's the difference?

Beitrag von AizazZaidee » 3. Jul 2018 19:01

Dear Professor,
Would you please kindly post some code example for delegation semantics. As you have explain today in the lecture "Visitor Pattern" and the concept of double dispatch, to my understanding that is delegation semantic. If not please clarify.

Also talking about Scala, to my understanding having mixins is delegation, if not please explain.

Thanking you in anticipation.

pmueller
Moderator
Moderator
Beiträge: 87
Registriert: 25. Jan 2018 11:14

Re: Java 8 Mixins VS Scala - What's the difference?

Beitrag von pmueller » 4. Jul 2018 08:51

Hi,

Delegation is discussed in the slides of the Decorator Pattern, beginning with slide 9. You created a thread concerning this yourself.
Whether you consider Java 8 Interfaces as mixins depends on your definition of mixin. Look here.
Scala's traits are still more powerful, since they can contain state themselves, which is not possible in Java, i.e. for your example you could directly store the AIrcraft and Cargo lists in the respective Traits and not in the AircraftCarrier class. You do not need the pattern with the abstract getter, like in your example. The blog post where you found this code has examples for this in other languages :).
In addition, there is the linearization. In your example this does not come into play, but if both Ship and Airport defined a method with a conflicting name/signature, you would have to manually resolve it in AircraftCarrier, whereas in Scala such a conflict would not happen because of the clearly defined order resulting from linearization, which enables Stackable Traits.
And you cannot create a Anonymous class in Java that implements several interfaces, and you have to write a lot of boilerplate to simulate the dynamic mixin behavior of Scala.

Best,
Patrick

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

Re: Java 8 Mixins VS Scala - What's the difference?

Beitrag von AizazZaidee » 5. Jul 2018 08:19

Mornin,
Thanks Patrick for your post, it looks like you have answered all the question in my head without asking... Thanks :D

Regards,

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

Re: Java 8 Mixins VS Scala - What's the difference?

Beitrag von AizazZaidee » 5. Jul 2018 14:39

I was referring to following part of code :P , other code is copied form http://hannesdorfmann.com/android/java-mixins :lol:

Code: Alles auswählen

public interface Ship {
    // To be implemented in subclass
    List < Cargo > getCargos();

    default void addCargo(Cargo c) {
        getCargos().add(c);
    }
    void removeCargo(Cargo c) {
        getCargos().remove(c);
    }
}

pmueller
Moderator
Moderator
Beiträge: 87
Registriert: 25. Jan 2018 11:14

Re: Java 8 Mixins VS Scala - What's the difference?

Beitrag von pmueller » 5. Jul 2018 17:07

Hi,

what is your question here?

Best,
Patrick

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

Re: Java 8 Mixins VS Scala - What's the difference?

Beitrag von AizazZaidee » 5. Jul 2018 17:10

Sorry for misunderstanding, please ignore this :roll:

Antworten

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