Switch类有什么用? [英] What are Switch classes used for?
问题描述
什么是开关类(从 org.eclipse.emf.ecore.util.Switch< T>
派生)?
What are switch classes (derived from org.eclipse.emf.ecore.util.Switch<T>
) used for?
javadoc 解释为
所有开关类的抽象基类.
An abstract base class for all switch classes.
这无济于事,因为我以前从未听说过开关类".
which does not help as I have never heared about "switch classes" before.
推荐答案
switch类是允许您基于模型对象(在这种情况下为EMF模型对象)选择和实例化类型的具体实例的类.).我所看到的示例表明,它对于为EMF模型实例化类型特定的适配器很有用.
A switch class is a class that allows you to choose and instantiates a concrete instance of a type based on a model object (in this case, an EMF model object). The examples I've seen suggest it's useful for instantiating type-specific adapters for an EMF model.
您可以通过重写doSwitch方法来使用它.例如,假设我有一个模型对象,并且我想实例化一个与我模型中的 type
值相对应的适配器对象:
You use it by overriding the doSwitch method. For instance, say I've got a model object, and I want to instantiate an adapter object that corresponds to the type
value in my model:
public class ExampleSwitch extends Switch<Adapter> {
public Adapter doSwitch(EObject eobject) {
if (eobject instanceof MyModel) {
switch (eobject.getType()) {
case TYPEA:
Adapter result = createTypeAAdapter(eobject);
if (result == null) {
return createDefaultAdapter(eobject);
}
return result;
case TYPEB:
...
default:
}
}
}
}
eclipse org.eclipse.emf.common.notify.AdapterFactory然后将使用它返回一个适配器.
An eclipse org.eclipse.emf.common.notify.AdapterFactory would then use this to return an Adapter.
public class MyAdapterFactory implements AdapterFactory {
public boolean isFactoryForType(Object o) {
return (o instanceof MyModel);
}
public Adapter adapt(Notifier notifier, Object type) {
return mySwitch.doSwitch((EObject)notifier);
}
}
我已从此处.我尚未对此进行验证,但是显然EMF生成器可以选择为您生成AdapterFactories和Switch类.
I've pulled this information from here. I haven't verified this, but apparently the EMF generator can optionally generate your AdapterFactories and Switch classes for you.
这篇关于Switch类有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!