Inner Class Good Code Structure

Started by
0 comments, last by ppgamedev 13 years, 11 months ago
Hello, I have come to a question that I require outside support with. I am creating a class that will parse some html and do some specifics that I can easily separate into 3 different classes. However since we have a large amount of classes and 2 of them will only ever be used inside 1 of them I have created 2 private classes. The problem arises in that the first private class is simply a container

private class Container {
  private DTO dto;
  private boolean found;
}
The second private class I want to create is one that holds a list of containers and does all logic involved with them.

private class ContainerList {
  private List<Container> containerList = new ArrayList<Container>();
  
  private boolean findID();
  private void makeNewDTO();
  private void deleteDTO();
  ..
Pretty much any logic that has to do with the list of Containers would be done in this private class. This could involve any finds for the entire Container (much of the data in the DTO's are unique). As well as calling outer methods that do specific add and removes for the DB. This could all be done in the outer method but for me it makes more since to organize it like this however I am not completely sure. So is it ever wise to have a private class nested inside another private class? Or should I simply just have a private container class and do any logic with that list inside the outer class (all private methods of course).
Advertisement
I would personally go for the private inner class.

But looking at the implementation of Container I wonder if it could be expressed in a different way.

Just having this in ContainerCollection seems an equivalent implementation:
private Collection<DTO> dtos = new ArrayList<DTO>();
private Collection<DTO> foundDtos = new ArrayList<DTO>();

(do you really need a List?, maybe it is a Set implemented with LinkedHashSet)

This topic is closed to new replies.

Advertisement