On page 93-4 of Effective Java, I came across the term mixin. But I am finding it difficult to visualise what a mixin actually is. Could anybody please help me out by providing an example of a mixin in Java?
You're referring to Item 18 of Effective Java - Prefer interfaces to abstract classes, and I believe the following section in particular:
Interfaces are ideal for defining mixins. Loosely speaking, a mixin is a type that a class can implement in addition to its "primary type" to declare that it provides some optional behaviour. For example
Comparable
is a mixin interface that allows a class to declare that it its instances are ordered with respect to other mutually comparable objects. Such an interface is called mixin because it allows the optional functionality to be "mixed in" to the type's primary functionality. Abstract classes can't be used to define mixins for the same reason that they can't be be retrofitted onto existing classes: a class cannot have more than one parent, and there is no reasonable place in the class hierarchy to insert a mixin.
Essentially, one of the key differences between specifying functionality in an abstract class and in an interface is that the interface version can be used in a number of different class hierarchies, whereas an abstract class can only be used in the one class hierarchy tree because Java only allows single-inheritance.
Traversable
with one abstract method forEach
and 10 concrete methods that provide functionality based on it (drop
, take
, filter
, map
,...).With interfaces you can declare all these methods, but any class implementing this interface will have to provide implementation for all of them instead of just 1.Abstract classes work great for this, but they are limited to just 1 as parent of a class and they put you in specific hierarchies.Not mixin –
Ygerne There is no such thing as mix-in in java since there's no way to add the a piece of code to classes in separate hierarchies. To do so would require multiple inheritance or a least Scala type traits.
In 'Effective Java'`s scope, it is mentioned just logically, without specific Java implementation. For example, a Comparable interface. It doesn't change your class purpose or confuse your api users. It just mixes in functionality for sorting and comparation. So, in a Java context I would narrow this to a Decorator pattern.
Another variation on a mix-in could be the following. Assume, you have:
interface IMyInterface
{
public void doStuff();
}
class MyClass implements IMyInterface
{
public void doStuff(){};
}
Now we want to 'mix in' some additional functionality. We add an abstract class:
abstract class AbstractMixInProvider
{
public abstract void doMixinStuff();
}
And we extend MyClass from AbstractMixInProvider:
class MyClass extends AbstractMixInProvider implements IMyInterface
{
public void doStuff(){};
public void doMixinStuff();
}
But, as I`ve mentioned above, trying to pull mix-in concept to Java looks ugly, because it is just play on words.
t. That why you can use multiple interfaces for your target class or one AbstractMixinProvider with multiple interfaces in it. That
s why I think, that calling all this stuff 'mixin' is weird. –
Peignoir © 2022 - 2024 — McMap. All rights reserved.