Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Usage Of Switch And Adapter Factory(ModelSwitch and ModelAdapterFactory)
Usage Of Switch And Adapter Factory [message #629954] Thu, 30 September 2010 06:29 Go to next message
vaibhav  is currently offline vaibhav Friend
Messages: 22
Registered: September 2010
Junior Member
Hi,

can anyone tell me what is the use of ModelSwitch.java generated when we generate Model code from genmodel?

I mean what is the use and How it exactly work? and How is related to ModelAdapterFactory?


Thanks in advance..

Best Regards,
vaibhav
Re: Usage Of Switch And Adapter Factory [message #629970 is a reply to message #629954] Thu, 30 September 2010 07:06 Go to previous message
Hallvard Traetteberg is currently offline Hallvard TraettebergFriend
Messages: 671
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
Previous Topic:RCP & EMF Compare
Next Topic:Is there a smart way to adjust/use the EMF code generator for instances of my ecore model?
Goto Forum:
  


Current Time: Wed Oct 16 06:00:16 GMT 2019

Powered by FUDForum. Page generated in 0.01511 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top