Instantiating Object dynamically

Asked

Viewed 305 times

0

Good morning, I need to instantiate a class according to the parameter received by a webhook, someone has some idea how to do?

It would be more or less that (some scenarios):

Client

  1. I receive JSON {"id":"1"}
  2. I must create a class reference ClienteDao who implements a collector interface with a collection method.
  3. My Object integration receives the collector object (ClienteDao) and calls the collection method, which queries the bank and parses for JSON.
  4. Integration object returns the JSON to the webhook.

Product

  1. I receive JSON {"id":"2"}
  2. I must create a class reference ProdutoDao who implements a collector interface with a collection method.
  3. My Object integration receives the collector object (ProdutoDao) and calls the collection method, which queries the bank and parses for JSON.
  4. Integration object returns the JSON to the webhook.

Cart

  1. I receive JSON {"id":"3"}
  2. I must create a class reference CarrinhoDao who implements a collector interface with a collection method.
  3. My Object integration receives the collector object (CarrinhoDao) and calls the collection method, which queries the bank and parses for JSON.
  4. Integration object returns the JSON to the webhook.

Excluded solutions:

  1. Series of commands if -> procedural
  2. Table with id and class, where I save the id of the webhook and the . class to instantiate. Making use of Reflection instancio the class -> sounds like gambiarra.

I am looking for an elegant solution and in the future I will not have headache to maintain.

  • I think a valid question is why do you want to do this? Wouldn’t it be better to have a callback different for each type of resource?

  • Thank you @Anthonyaccioly ! So I’m integrating with an order tracking tool. The platform uses a webhook to handle all requests, this way I.

  • Okay, in that case I think we should share more details and an example code... The trivial way to do this kind of dispatching would be to use a map ID -> Object Integration with the correct dao. Another option would be to do Programmatic lookup annotating the Daos with annotations @MyWebhook(forId="x") or production methods everything depends on your architecture and framework.

  • @Anthonyaccioly is exactly what I’m looking to do with program lookup!!! I’m using Spring Boot, I haven’t implemented anything yet... the examples above are just case study to explain what I needed. You can explain me better how to do with Program Lookup?

  • Hi Thiago, there are several ways to do this in Spring... Since lookup by bean name on Application Context, passing by Factories, service locators, Qualifiers, self-wired on a map, etc., etc., etc. The ideal solution will depend on the architecture of your application and the taste of the freques. I suggest you mature the idea, implement something and when you get to the point of having at least one MCVE edit your issue with the code, it is easier to discuss this kind of thing on top of a concrete example.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.