|
Re: Usage Of Switch And Adapter Factory [message #629970 is a reply to message #629954] |
Thu, 30 September 2010 07:06 |
Hallvard Traetteberg Messages: 673 Registered: July 2009 Location: Trondheim, Norway |
Senior Member |
|
|
On 30.09.10 08.29, vaibhav wrote:
>
> can anyone tell me what is the use of ModelSwitch.java generated when we
> generate Model code from genmodel?
When you need to handle an instancee of some EClass in an EPackage it
potentially can be any EClass, then you'd normally have to write a long
if (eObject instanceof EClass1) {
....
else if (eObject instanceof EClass2) {
....
} ... {
....
}
You can think of the switch as having such a structure, and that each
branch calls methods named caseEClass1, caseEClass2, etc. So instead of
writing the if structure yourself, you subclass the switch class,
implement the corresponding methods. When you give it the object to
handle, by calling doSwitch(object), one of your case methods will then
be called. The important part is that it tries methods for subclasses
first, and relieves you from having to think about the order of if branches.
I didn't think the switch class was so useful myself, until I had to
pretty-print a nested structure of instances of subclasses of a common
Expression class. I implemented a subclass of my switch class and
implemented methods like
@Override
public Object caseIfOp(IfOp object) {
...
}
@Override
public Object caseBinOp(BinOp object) {
...
}
@Override
public Object caseUnaryOp(UnaryOp object) {
...
}
Cleaner than the if structure, IMO.
Hallvard
|
|
|
Powered by
FUDForum. Page generated in 0.02843 seconds