jeudi 28 mars 2019

design service class for different representation of same POJO

We are on one requirement to design service class which transform a base class instance (I) into different class O1 and O2 object and each of them can be converted into text, xml, json, yaml or zip before sending back to client.

Current flow

  1. We have 2 endpoint to get output (o1 and o2)
  2. Endpoint e1 call to get(id) and endpoint e2 call to read(id)
  3. We read from DB using DAO layer, common for both get and read functions
  4. After DBB call in method get(id) we change db object to produce o1 & similarly read(id) to o2

New Requirement

  1. Endpoint which can transform o2 into yaml then zip it and return back to client.
  2. Endpoint to produce list of o1 and o2.
  3. Endpoint to produce list o2 dump into yaml then zip it and return back to client.
    class Base {
      ...
    }

    class O1 {
      ...
    }

    class O2 {
      ...
    }

    class Service {
      Dao d;

      O1 getO1(id) {
        return Converter.toO1(read(id));
      }

      Base read(id) {
        return d.byId(id);
      }

      O2 getO2(id) {
        return Converter.toO2(read(id));
      }

      List<o1> listO1(){
        List<O1> list = new ArrayList<>();
        Consumer produce = b -> { O1 o1 = Converter.toO1(b); list.add(o1);}
      }

      List<o2> listO2(){
        List<O2> list = new ArrayList<>();
        Consumer produce = b -> { O2 o2 = Converter.toO2(b); list.add(o2);}
      }

      private void read(Consumer produce) {
        d.all().forEach(produce);
      }
    }

Need suggestions on designing pattern and principle to apply for Service class to address this requirement.

Aucun commentaire:

Enregistrer un commentaire