在Java中实现Mixin?

55 java mixins

使用Java 6,我如何实现mixin?在Ruby中它非常简单易行.我怎样才能在Java中获得相似之处?

Dan*_*jul 21

您可以使用CGLIB.Mixin类能够从几个接口/对象委托生成动态类:

static Mixin    create(java.lang.Class[] interfaces,
                        java.lang.Object[] delegates)
static Mixin    create(java.lang.Object[] delegates)
static Mixin    createBean(java.lang.Object[] beans) 
Run Code Online (Sandbox Code Playgroud)


Mar*_*tin 13

我会说只使用对象组合.每次想要引入新功能时,将另一个对象作为成员组成类.如果要创建所有相同类型的混合类,可以使用数组作为成员对象,其中每个元素与所有其他元素组成,并且可以分派到特定元素.


Bra*_*pit 13

默认方法

我知道Java 6的问题,但在Java 8中我们将有一个相当不错的选择:默认方法.

我们将能够添加接口方法的"默认"实现,因此我们可以添加新方法,而不会破坏实现接口的每个类.

只要你的mixin不需要状态,你就可以在界面中编写代码.然后你的班级可以根据需要实现尽可能多的接口和繁荣,你有mixins.

这是滥用系统吗?一点点,但它没有涉及任何多重继承问题,因为没有状态.

当然,这也是这种方法的最大缺点.


Joh*_*iss 8

由于Java仅支持单继承,因此这是不可能的.看看WP:Mixin.

编辑:由于关于接口的评论:关于mixins的很酷的事情是你可以在不编写组合代码的情况下组合它们.使用接口,您必须自己实现组合的功能(除了可以扩展的一个类)!

  • HALF可能;-) (5认同)
  • 这是使用接口的一半. (2认同)

Bra*_*pit 7

最简单的方法是使用静态导入.它允许代码重用"看起来"像它是类的一部分,但实际上是在其他地方定义的.

优点:

  • 真的很容易
  • 你可以随意'混合'尽可能多的静态导入

缺点:

  • 静态方法无法访问'this',因此您必须手动传递它
  • 无状态:您的静态方法不能拥有自己的实例字段.它们只能定义自己的静态字段,然后由调用静态方法的任何对象共享.
  • 无法在客户端类上定义公共方法(将代码混合到其中的方法).在Ruby中,导入mixin实际上会将这些公共方法定义为类中的公共方法.在Java中,在这种情况下继承将是更好的解决方案(假设您不需要扩展多个类)

例:

import static my.package.MyHelperUtility.methodDefinedInAnotherClass;

public class MyNormalCode {
    public void example() {
        methodDefinedInAnotherClass();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 请不要这样做.您的对象很快就会变成精神分裂症,请参阅http://en.wikipedia.org/wiki/Schizophrenia_(object-oriented_programming).此外,使用静态方法组合对象会使它们难以测试. (6认同)

Edd*_*die 5

从某种意义上说,Ruby混合相当于Java抽象类,不,你不能在Java中实现混合.您可以通过使用接口来完成,因此在您的混合中绝对没有定义代码,但是您无法直接实现与Ruby混合中相同的行为.


Nic*_*man 5

更新:Qi4j现在是Apache Polygene,https://polygene.apache.org

Qi4j's definition of Mixins are probably quite unique, as it doesn't start with a base class. By going to that extreme, a whole new paradigm of how applications are built up emerges, and we call that Composite Oriented Programming. The Composite is the 'object' equivalent and not only are Mixins wired together, but also Constraints (validation), Concerns (around advice) and SideEffects (can't alter the method outcome).

So I think Qi4j has a very strong Mixin story to tell. Mixins can be 'typed' or 'generic', they can be public (accessible outside the composite) or purely private (within the Composite). Qi4j strongly defines what Properties are, and goes on to have built-in persistence, which doesn't leak the storage implementation into your domain (caveat; Qi4j leaks to your domain). And once persisted entities enters the picture, strong definition of a Associations are also required (and included in Qi4j).

See http://www.qi4j.org/state-modeling.html for a good overview.

In Qi4j, ONLY Mixins have state. Constraints/Concerns/SideEffects can't have state (if they do they need to referens a private mixin).

要在Qi4j中定义组合,可以在类型本身上进行结构化,也可以在创建运行时模型时的引导时进行.

结构上;

@Mixins({PetrolEngfineMixin.class, FourWheelsMixin.class})
public interface Car extends HasEngine, HasWheels, EntityComposite
{}

public interface Car
{}

public class CarModuleAssembler implements Assembler { public void assemble( ModuleAssembly module ) { module.entities( Car.class ) .withMixins( PetronEngineMixin.class, FourWheelsMixin.class ); } }

然而,这只是触及Qi4j中的特征表面.