-
Notifications
You must be signed in to change notification settings - Fork 0
2. Custom Ecore Factories
EMF creates a package and a factory class for every EPackage when generating model code from an Ecore metamodel. These factories allow creating instances of the types contained in the package. Every factory consists of an interface and an implementation class. While it is not necessary to use the factories for the instantiation of the types in the package, EMF highly encourages it. Additionally, many tools like model transformation languages use these factories
The factories in the Ecore model code are creating instances of the implementation classes of the Ecore Code. If we want to use our ecorified Code as we would normal model code generated from Ecore metamodels, the factories have to create instances of the classes from the origin code. To achieve that, we have to create adapted factories. It is important to keep the original factories as well because they are needed to create instances of the Ecore code implementation classes for the integration code. As discussed in previously, the wrapper classes of the integration code delegate certain methods to the implementation classes of the Ecore code.
To keep the original factories intact, we copy them into a subpackage of the factory package. While the copies are not being altered, we change the original factories to create origin Code instances. We adapt the methods in the implementation classes of the factories to return the correlating class of the origin code instead of the Ecore code implementation class. The method signature of the implementation class and the interface does not have to be changed because both the classes from the origin code and the Ecore code implement the interfaces of the Ecore code.
For the Ecore code class ECustomer of the example the original instantiation method in the factory class looks like this:
public ECustomer createECustomer() {
ECustomerImpl customer = new ECustomerImpl();
return customer;
}An instance of the class ECustomerImpl is created and returned. The method declares the return type ECustomer.
This can be easily altered to create an instance of the class Customer of the origin code by changing one single line in the method body:
public ECustomer createECustomer() {
Customer customer = new Customer();
return customer;
}While the declared return types stay the same, the actual return type is changed. After copying the factories and adapting the original ones the ecorified code can be used as one would normal model code. While the interfaces of the adapted factories do not show any change, internally, classes of the origin code are instantiated.