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
我会说只使用对象组合.每次想要引入新功能时,将另一个对象作为成员组成类.如果要创建所有相同类型的混合类,可以使用数组作为成员对象,其中每个元素与所有其他元素组成,并且可以分派到特定元素.
最简单的方法是使用静态导入.它允许代码重用"看起来"像它是类的一部分,但实际上是在其他地方定义的.
优点:
缺点:
例:
import static my.package.MyHelperUtility.methodDefinedInAnotherClass;
public class MyNormalCode {
public void example() {
methodDefinedInAnotherClass();
}
}
Run Code Online (Sandbox Code Playgroud)
从某种意义上说,Ruby混合相当于Java抽象类,不,你不能在Java中实现混合.您可以通过使用接口来完成,因此在您的混合中绝对没有定义代码,但是您无法直接实现与Ruby混合中相同的行为.
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中的特征表面.
| 归档时间: |
|
| 查看次数: |
29519 次 |
| 最近记录: |