Avoiding downcasting when trying to extend a Java object

I get several objects of type Foo from an external API call. Locally, I want to process these objects with a little added information, so I have a subclass of FooSon that adds these extra fields. How can I convert all of the objects that I get into my new inherited type? Downcasting does not seem to be an option because these objects are not really FooSon .

The only solution I developed is to create a conversion function that takes a Foo object as an argument and then copies all public / protected values ​​to a new FooSon object, which is then returned.

Disadvantages:

  • Loss of information (private values)
  • You need to adapt the conversion function if Foo is ever changed.

The Foo class does not implement the copy constructor or clone operator. I have the source code for Foo, but I would like to avoid changing it in order to maintain compatibility with future versions. However, if this is the only viable alternative, I would modify the Foo implementation to get what I need.

+4
source share
5 answers

FooSon can have a field in it that is Foo. then just assign the return value to this field. You can then create methods in Fooson that delegate their calls to the Foo field for the information you need from Foo from the outside.

+4
source

I think the decorator pattern should work here:

class Foo implements FooApi {...} class FooSon implements FooApi { private FooApi decoratedFoo; private String additional; public FooSon(FooApi decoratedFoo) { this.decoratedFoo = decoratedFoo; } ... } 

but you can only do this if you have an interface for your Foo object.

+4
source

Perhaps I did not fully understand the problem, but why, instead of subclassing Foo (inheritance), do you not save Foo as a field type in FooSon (composition)?

In any case, if you cannot change access control to the Foo type because objects are defined in an external library, you cannot directly access private fields (this is exactly a property of a private field). If these private fields can be accessed from the getters and seters method, then wrap these methods in your FooSon class.

0
source

Good, since you have already discovered that in this situation, easy casting is not applicable. I see the following options:

  • The external library provides some factory mechanism that you can use to create FooSon instead of Foo .

  • The receiver wraps Foo objects in FooSon objects, possibly using a delegation pattern.

  • The receiver logically attaches additional information to the Foo objects using a card with the Foo key.

0
source

You should not play down, you have no guarantee that it will be successful, and there are more effective ways to solve this problem. For one, you can wrap the Foo object in another class and delegate the corresponding method, the callback of the Foo object.

I suggest you take some time to make sure you understand some of the basic concepts of OO. In particular, google for “composition versus inheritance”, this link seems to be a good explanation.

0
source

Source: https://habr.com/ru/post/1345422/


All Articles