I have a tricky problem. And being new to CORBA, I'm unable to get out of it.
How can I instantiate an implementation object from another implementation?
Usually, if I have an interface A. I would create an A_Impl class (in a A_Impl.java file), extending from the A_POA class generated from the idl.
Then, on the server side I would do something like this :
AImpl my_a_impl = new A_Impl ();
org.omg.CORBA.Object ref = orb.activate_object(my_a_impl);
A my_a_object = A.narrow(ref);
But what when one of the methods of another object B needs to return A?
In my B_impl class, I don't have access to the orb and thus cannot get a reference to my object by using the activate_object method.
How can I then return such an object?
Any help would be greatly appreciated.
Thanks in advance !
CORBA already offers a function that can be used to get an existing ORB reference - ORB_init(). For most ORBs, that function behaves like a singleton and will return an existing ORB object, assuming you use the same ORB ID each time you call it.
Put a reference to the current active orb in a singleton class, that you can reach from very object *_Impl.
It seems like (for a non comprehensible reason for me), constructors are not generated by the idl.
Creating constructors allows me to simply pass objects as references and make them visible to my classes.
Yes... as simple as that :)
Related
I have a class that's essentially like:
class Child extends Parent {
public void reinitialize() {
super(); // illegal
}
}
Basically, I want to call the constructor again to reinitialize. I can't refactor out the initialization code into its own method, because Parent is a library class I can't modify the source of.
Is there a way to do this?
No, there is no way to do this. Even at the JVM bytecode level, a chain of <init> methods (constructors) can be called at most once on any given object.
The usual answer is to refactor the code out into a normal instance method, but as you said, this is impossible.
The best you can do is to find a way to redesign to get around the need for reinitialization. Alternatively, if there's a specific behavior in the parent constructor you need, you might be able to duplicate it yourself.
The only work around for this is to either
create a new object each time you need to "re-intialise" it.
use delegation instead of inheritance, even if you have to use both. By using delegation you can replace the instance.
create a re-initialise method which does much the same thing as the parent constructor. e.g. replace fields or clear collections, using reflections if you have to.
One way to do this is provide a static method which returns a new Child object. Alternatively, you can simply create a new Child object in the client code. Either way, it sounds like you will be unable to reuse an existing object.
There are several ways to achieve this. One of them is create another method, for example "init". This "init" method should be invoked from either the constructor or the reinitialize method.
I am trying to see if there is a design pattern that can solve this:
I have created an abstract class A with properties a,b and c. I have another class B that extends the class A and adds more properties: x,y,z. Then I have another class C that again extends A and adds i,j,k. Lastly I have a factory method that determines which instance to create B or C and thats the part where I need help. How or what should this factory method return: If it returns an instance of A than I wouldnt know which specific instance was created B or C? I need this factory method to create a concrete instance. Now, I know that I could write static method like createB or createC but I am looking for a more general solution maybe another design pattern here.
Update: The reason I want to know the concrete class is that I need to pass this object to a frontend jsp. That JSP would need to know what specific class was instantiated so it can call the appropriate getters.
I would Keep Factory Pattern as it should. so the return type would be the Abstract A class. B & C Should also inherit their properties through Proxy Pattern.
So Make CAble & BAble Interfaces and use C & B as instances of A (as they're both generated by the same factory), then cast B & C to act as their Interfaces Describe.
Cheers!
UPDATE:
I think I've figured what you need, picture the following:
In a School Page.
There is a general view of Students & Professors, both have common and individual fields, but the common request is schoolMember.
class SchoolMember // The return type of your Factory
-name
+getView():SchoolMemberView // Will be used by the View//View Model
Interface Professor
getProfession()
Interface Student
getSemester()
class FacultyMamber: SchoolMember,Professor
-profession
+getView():SchoolMemberView
class UniStudent: SchoolMember,Student
-semmester
+getView():SchoolMemberView
By the sound of your description both B and C have nothing in common with A. You've mentioned that each class has particular properties but nothing about what they have in common / how they are related. If there is no relationship between your classes you shouldn't be using inheritence.
However, if there is a common relationship which you have not mentioned in your question, the factory method pattern would probably be what you're looking for.
The only thing that comes to mind when reading your problem is the abstract factory pattern:
http://en.wikipedia.org/wiki/Abstract_factory_pattern
Factory method should return an instance of A. If you need to know if concrete object is B or C, you can use instanceof operator, which is Reflection pattern. Ideally, you shouldn't need to know if you have B or C; that logic should all be handled by polymorphism of methods in A.
Exactly the point mentioned as a warning in the description of Factory pattern.
When you design an application just think if you really need it a factory to create objects. Maybe using it will bring unnecessary complexity in your application. Anyway if you have many object of the same base type and you manipulate them mostly as abstract objects, then you need a factory. I you're code should have a lot of code like the following, reconsider it.
if (genericProduct typeof ConcreteProduct)
((ConcreteProduct)genericProduct).doSomeConcreteOperation();
Source : http://www.oodesign.com/factory-method-pattern.html
I would suggest a Builder pattern that will know what object to build based on the specialized field set to the Builder.
As we know, servlet's get method uses the reference of HttpServletRequest(interface) variable request and this request variable is able to access method of it's implemented class.
But now when I have an interface Sanjeev which contain a show() method, I implement it in a class xxx. Now in another class I am creating a reference of the interface like this
sanjeev sa =new xxx();
sa.show();
It's working, but if I do this
sanjeev sa;
sa.show();
it's not working whenever in case of HttpServletRequest(interface) request work. Why?
Please tell me, I am very confused...
Thanks in advance.
If you do this:
sanjeev sa;
sa.show();
you'll get an error because sa doesn't refer to anything. You need to initialise an instance of a class before you can call instance methods, and you're not doing that. Something that doesn't exist can't perform an action.
In java if some variable/object (here sa) is not initialized no memory will be allocated to it.. and when you are calling sa.show() here sa means nothing as its not initialized... basic of java "A variable/object should be initialized before using
I have a class named as order in package com.abc
I have another class named as order in different package in different project named as com.bcd
Both order classes have same code get, set functions and variables with no changes.
Then I have objects
com.abc.order obj1 = new com.abc.order();
com.bcd.order obj2 = new com.bdc.order();
Now I have to pass arguments to a function and it takes only obj2 as parameters
How can I convert type of obj1 into type of obj2
I cannot change parameters for the function.
Looking forward to your reply.
thanks.
Have a look at Apache Commons BeanUtils to copy properties from one object to another.
http://commons.apache.org/beanutils/v1.8.3/apidocs/index.html
Something like
BeanUtils.copyProperties(obj2, obj1)
should do the trick.
You have to copy each field. You can use a BeanIntrospector to help you do this.
You could overload the function, simply by typing it again in your code, but this time so it will take instances of com.abc.order instead. Keep the other one too of course.
If you have defined these two functions, it will work. Problem is duplication. I've never seen a situation like this before and it doesn't sound like good coding practice to duplicate a class and use both in the same code. Can you cast them into each other? like (obj1) obj2? I don't think you can but I'm not 100% sure...
The best solution, if you can is to eliminate one of the two classes especially if they are identical.
Otherwise rename them (first letter capitalised, if possible), again if you can.
Finally, if you cannot perform either of the above, write a conversion method on one or both classes, resolve between the classes where used by retaining their package in all usages.
import com.abc.order;
abc.order obj1 = new abc.order();
bcd.order obj2 = new bdc.order();
you can make the class com.bdc.order subclass of com.abc.order
can you clarify why u want to do this ?
I've got an interface MigrateScenario with its methods. I also have its implementation MigrateScenarioImpl. The instance of MigrateScenarioImpl is being serialized and then passed as a byte[] to some web-service. On the service side, when I try to do this:
Object obj = (new ObjectInputStream(digitalObject.getContent().getInputStream())).readObject();
MigrateScenario mgrScenario = (MigrateScenario) obj;
and what I get is that MigrateScenarioImpl class is missing in the classpath. The obj variable contains that object and the interface should blindly ask for the corresponding methods, since the object "knows itself" how to execute them. However, when I add the MigrateScenarioImpl to the classpath, then all works fine. Am I missing something here?
Would appreciate any help, thanks!
Knowing just the interface doesn't help the JVM know what to do. It needs the Impl to know what to actually do, not just what methods are available.
You can't deserialize a class without having that class known to the classloader (e.g. by being on the classpath). The code for the class itself is not serialized, it is expected to be known on the other end.
When you deserialize the object, Java needs to instantiate and initialize in an instance of the class. Interfaces cannot be instantiated, so Java must instantiate the implementation class. The serialization of a class contains instance data (the values of non transient instance variables), not the code of implemented methods, so there are no methods for the interface to point to.