So I have a POJO class let´s call it: InnerDomainObject.
Then I have an object representing this object, with a few more fields, for communication towards different clients (it s an API DTO): OuterDomainObject
Because the DTO has in fact all of the POJOs fields, I made OuterDomainObject inherit from InnerDomainObject.
Now I need to somehow cast InnerDomainObject to OuterDomainObject --> not possible.
I want to avoid writing a constructor iterating through all the fields.
I want to avoid writing useless code.
I just want OuterDomainObject to be created out of InnerDomainOBject´s values and then add some to it before sending it to the client.
What´s the best way of doing this ?
You sound like you are using the Adapter Pattern. You shouldn't need to cast an InnerDomainObject to an OuterDomainObject. You should use composition: the OuterDomainObject should hold a reference to an InnerDomainObject, which will likely be passed into a constructor. When a client invokes a method on an OuterDomainObject, if that method exists in InnerDomainObject, the OuterDomainObject should call that method on its instance of InnerDomainObject. Instead of casting an InnerDomainObject foo to an OuterDomainObject, just create a new OuterDomainObject and pass in foo: new OuterDomainObject(foo). You will need to write some simple glue code, but the result is very clean.
Related
I'm a little confused about the returned object from my GoogleCloudEndpoint.
My Android App uses an Object e.g TestObject with something like this path: com.example.classes.TestObject. My GoogleCloudEndpoint uses this class too and returns it in some methods, but returned objects of those methods are always another generated class like this: `com.example.testApi.model.TestObject.
Now in my App this is very confusing because I have to handle two nearly identical objects with the same name. The only difference between the object types is, that the generated object has private fields with getters & setters, while the original one had public fields. Obviously there is not way to cast those object to the other.
Is there any way to transform this generated object back to the original one? Or should I only use the generated one in my App?
You have to create your own helpers to transform back and forth. The model classes are subclasses of a generic JSON container object.
Serialization makes sense as an instance method - an object might reasonably be able to serialize itself. An object should only ever be in a valid state, and all valid states of an object should be permissible to serialize. There is nothing invalid about this idea.
But deserialization does not make sense as an instance method. No part of an object's state should have any bearing on the process of constructing another object from data. There is no class foo such that you need a constructed foo in order to construct a foo.
So my question is, does standard java have a pre-existing set of interfaces/facilities to facilitate static deserialization? If you implement the instance-based approach, your deserialization "just works" (as much as anything does) with anything that works with Java's default deserialization ability.
Is there anything built in, to use classes as factories for objects of that class, constructed from serial data? Is there anything in Java I could pass a class to, such that this facility would know to call some static method to deserialize to construct an object from its flat form?
The deserialization instance method readObject is private. There is no way to call it from the outside. You could call it from one of your instance methods, but that would be very strange and I'd question why you'd be doing that in the first place. You say:
No part of an object's state should have any bearing on the process of constructing another object from data.
True, but I don't see why you think this would be an issue. There's no way you could call readObject from the outside (unless you call it from some other public method, which as I said, is kind of iffy) on an instance that you have already created. When you deserialize, you will most probably be using ObjectInputStream, which will use the no-args constructor to create a new instance, and then will hydrate that object using the data from the stream (when you call ObjectInputStream#readObject). So there is no question of the state of the instance affecting deserialization, because what you get back is an instance created from the serialized data (as Object, but you will then cast it to the concrete type).
In effect, readObject behaves somewhat like a constructor, except that it uses previously-serialized data to create an instance of an object. Extending the analogy, your question wouldn't make sense because you would be asking "Why does creating an object using the constructor have anything to do with the state of the instance?". The question of state doesn't even apply because you don't even have an instance! Similarly, state doesn't come into play with readObject because can never* deserialize and create an instance by using an existing instance.
If you want greater control over serialization, you can override readObject and writeObject from Serializable within your class if you want to handle things in a special way. You can exert greater control over how the data is written out by implementing Externizable and providing implementations for readExternal and writeExternal.
In your second question you're wondering what the "something" is that calls readObject. The "something" is reflection; ObjectInputStream will check to see if the class has a readObject method. If you've provided your own implementation, it will call that. Otherwise it will call defaultReadObject (which contains the logic for default serialization).
As far as built-in factories for deserialization, there isn't anything and I haven't really felt a need something since the standard serialization/deserialization approach seems to work well.
If you want more information on this, I suggest taking a look at the serialization specification for a comprehensive and in-depth view of how Java tackles serialization, and specifically Object Input Classes for your particular question.
*The only way state comes into it is if you do something strange like calling the readObject method from some other instance method (which would have to take in an ObjectInputStream), and then you have custom logic that performs deserialization based on the state of the existing instance. In other words, the only way the object's state has any bearing on deserialization logic is if you explicitly write it that way. Again, as I mentioned before, that would be very strange code, with a whole lot of caveats and of minimal value.
This may sound like a silly question, however i am trying to test my game under different circumstances using reflection. I was wondering if their was anyway to dynamically create an object to contain certain methods, i know i can use proxies, but then i am limited to the methods declared in the interfaces i choose to use in the proxy so i have to create a new interface for each thing i want to add to my object that i am creating. I am hoping to access each method using reflection. I know there are libraries that do this so i am sure that this is possible and i am hoping to not have to install libraries, as i will have to deal with a new api.
In languages like C, you can pass function references as parameters to another function or procedure. Is this what you are referring to? You want to pass a reference to a function to a method about which the method may not have advance knowledge?
You can't pass function references as a parameter in Java. It isn't allowed. But the workaround for this is exemplified by the abstract factory pattern. This pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.
This problem comes up quite a bit actually. Take this instance model view. I have 2 objects. Simulation object and render object.
The class ObjectRSim (Object, designated as Render Sim) is something like:
class ObjectRSsim {
var simObject:ObjectSim;
}
ObjectRSim, uses properties/invalidation of ObjectSim.
There are objects that inherit ObjectRSim and ObjectSim:
PosObjectRSim and PosObjectSim (positional objects..)
class PosObjectRSim extends ObjectRSim {
var posSimObject:PosObjectSim;
}
or...
class ObjectRSim {
var simObject:Dynamic; //Dynamic is untyped Type from haxe, the compiler does not check calls to a Dynamic object.
}
Should I have a reference to each type in the inheritance hierarchy of ObjectSim in the ObjectRSim hierarchy classes, or should I just use simObject:Dynamic?
It will be totally wrong to use Dynamic for that case. It's frequently slow(for every haxe target except javascript I guess), not inlined, and is normally used only for interacting with underlying platform or working with serialization(YAML, JSON etc.).
Talking about solving the problem...
If there aren't thousands of this objects planned, I'd probably just another field for lower-level class object. It's not the clearest way from OOP perspective, but it frequently results in simpler and clearer code for me.
Casting is another option(I personally don't like).
Parameterization would probably be the best way to handle that, but I'm not sure if we already have type restricted parameterization in haxe.
Parameterize the dependent class and the dependency member.
In xstream for java, is there a way to deserialize an object by ensuring that it goes thru a specific constructor with parameters?
XStream (or deserialization in general) doesn't call constructors. (Except in the rarely used Pure Java Mode, when it calls the default constructor.)
You need to use the readResolve() method if you want to initialise transient fields.
However you can write your own converter, and then you can do whatever you want. This approach works best if you have one specific class that you want to apply this to.