0

I have a program that recruits witness accounts of a crime scene from various subjects. Every kind of subject has its own Report class. For example:

public class EyeWitnessReport extends AbstractReport {
        private String details;
        private String relation;
        private QuestioningResults questioningResults;
        private Integer stressLevel;
        private EnumSet<CrimeFacts> crimeFacts;
        private Religion religion; //to see if swearing on the Bible is an issue
    }

public class ExpertWitnessReport extends AbstractReport {
    private Expertise fieldOfExpertise;
    private boolean relatedToAccused;
    private List<Conflict> conflicts;
}

The main function of my app takes all the reports and merges them together into one CrimeScene class that contains all the details of the crime scene (by priorities, etc)

 public CrimeScene mergeReports(List<AbstractReport> sources){
        //takes all the reports in sources and returns one CrimeScene with the most relevant fields
}

My question is, the CrimeScene class is basically made up of data from the reports, after certain logic. It has 70 possible fields (the fields of every type of report) although on average only around 10-20 are filled. Plus it has 2-3 fields of it's own (creationTime, reportingOfficer, etc...)

How should I go about implementing it? Is it a better implementation to make CrimeScene a regular class (70 hardcoded fields, of which no more than 20 will be filled on average), or a dynamic class, using DynaBean, Map<String, Object> or something of the sort?

chimera_girl
  • 155
  • 13
  • Have you thought about injecting the reports? I'm talking about dependency injection. That way you can have an array or whatever structure you want filled with any kind of report. Also, please provide sample code. – Ivan Kukic Feb 19 '19 at 20:56
  • Post the class, so we can take a look and give you a decent answer. – LppEdd Feb 19 '19 at 20:59
  • @IvanKukic thanks. i dont want to inject the reports because rather than having CrimeScene hold all the reports, I wants its fields to contain data that comes from the reports after applying a certain logic – chimera_girl Feb 19 '19 at 21:28
  • Check out [strategy pattern.](https://en.wikipedia.org/wiki/Strategy_pattern) – Ivan Kukic Feb 19 '19 at 21:31

1 Answers1

0

It is better to use class with properties or maybe some composite class which would allow you to limit number of properties in a single class due to composition. With this approach you (and any other developer) will know exactly which properties and of which types does object have.

If you go with map then you will lose type safety because you will have to use Map<String, Object> to accommodate values of properties of different types. Also with map approach the only way to know which properties exist (which keys are added to the map at runtime) is to debug your code.

Ivan
  • 8,508
  • 2
  • 19
  • 30
  • what about keeping the properties in a json file or something? – chimera_girl Feb 19 '19 at 21:39
  • You will either have a json file with 70 properties and convert in to Map losing all type safety. Or convert json into Java object but for that Java object you will need to develop Java class with all those 70 properties – Ivan Feb 20 '19 at 20:13