I am currently having below parent child relation java objects
JCL > JOB > steps > step > dd
Class looks like below
public class Jcl {
Private Job job;
}
public class Job {
private String jobname;
//Below is direct property of job object
private List<Jobparam> jobparam;
//Below is child object of the job object
private List<Step> step;
}
public class Step {
private String stepname;
//Below is direct property of step object
private List<Stepparam> stepparam;
//Below is child object of the step object
private List<Ddstatement> ddstatement;
}
public class Stepparam {
private String name;
private String value;
}
public class Jobparam{
private String name;
private String value;
}
I am using getters and setters to create objects with new instance of each object.
Job job = new Job();
ArrayList<Jobparam> myList = new ArrayList<Jobparam>();
job.getJobparam().addAll(myList);
Step step = new Step();
ArrayList<Stepparam> myList = new ArrayList<Stepparam>();
jcl.getJob().getStep().add(step);
And also jobparam and stepparam have same property.(I want to reuse but donno how)
I feel I am creating each new object every time and this long coding can be avoided by java design pattern.
Any suggestion how should I proceed to make simple code would be helpful.
public class Jcl {
Private Job job;
}
Snippet above isn’t really parent-child relation. You’re only defining the attribute of class, which you have defined.
However such hierarchical relations could be used to simplify your last two classes eg.:
public class Parent {
private String name;
private String value;
}
public class Stepparam extends Parent {
// whatever makes it different from Jobparam
}
public class Jobparam extends Parent {
// whatever makes it different from Stepparam
}
I’d suggest you to google object relations in java
or check out this article.
Answer:
I would suggest creating a common class for the parameter types since they are the same
public class Parameter {
private String name;
private String value;
}
and then to declare the job and step list members to be
List<Parameter> ...
I would also suggest adding wrapper methods for adding stuff to list, it makes the code clearer and the List is hidden and it will be easier to change to another collection type if you want. For instance
public class Job {
//other stuff
public void addJobParameter(Parameter parameter) {
jobparam.add(parameter);
}
// and/or
public void addJobParameters(Collection<Parameter> parameters) {
jobparam.addAll(parameters);
}
Answer:
You can create a constructor for classes Job
and Step
and inside them initialize the list like below:
public class Job {
private List<Jobparam> jobparam;
private List<Step> step;
public Job() {
this.jobparam = new ArrayList<Jobparam>();
this.step = new ArrayList<Stepparam>();
}
}
For classes Stepparam
and Jobparam
you can create a base class and extends it, but it seems me that a Map<String, String>
jobparam and a Map<String, String>
step could be more appropriate of List<Jobparam>
jobparam and List<Step>
step, so you should not have to define the classes Stepparam
and Jobparam
. You can check map documentation at Map.