Instantiate Object dynamically

0

Hello, I need to instantiate a class according to the parameter received by a webhook , does anyone have any idea how to do it?

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

Customer

  • I get JSON {"id":"1"}
  • I need to create a reference to class ClienteDao who implements a collector interface with a collection method.
  • My integration object receives the collector object ( ClienteDao ) and calls the collection method, which queries the database and makes the parser for JSON.
  • Object integration returns JSON to webhook .
  • Product

  • I get JSON {"id":"2"}
  • I need to create a reference to class ProdutoDao who implements a collector interface with a collection method.
  • My integration object receives the collector object ( ProdutoDao ) and calls the collection method, which queries the database and makes the parser for JSON.
  • Object integration returns JSON to webhook .
  • Cart

  • I get JSON {"id":"3"}
  • I need to create a reference to class CarrinhoDao who implements a collector interface with a collection method.
  • My integration object receives the collector object ( CarrinhoDao ) and calls the collection method, which queries the database and makes the parser for JSON.
  • Object integration returns JSON to webhook .
  • Solutions excluded:

  • Command string if - > procedural
  • Table with id and class, where I save the id of the webhook and the .class to instantiate. Using reflection instantiates the class - > sounds like gambiarra.
  • I'm looking for an elegant solution and that in the future I will not have headache to maintain.

        
    asked by anonymous 23.02.2017 / 14:26

    0 answers