Search code examples
javadesign-patternscomposite

Is a good idea use composite pattern for this?


I'm making a code to make a custum TV Dayparting.

So i need to make this schme of data:

  • A dayParting can have multiples DayParts
  • A DayPart can have multiples TimeParts.
  • DayPart and DayParting have a begin hour and an end hour
  • TimePart it is only an hour and have the program to be shown at that time.

At the first time i just use Lists to do the Job, but i started to think that was too "not open to extension" and too "to implementation and not to intarface" solution, so i started to search an alternative solution to this problem.

I was thinking in use the composite pattern, the pattern provides flexibility, open the expansion and allows me to work with composite and individual objects uniformly.

So, i think my classes should be:

class AbstractDayparting{ //
}
class DayParting extends AbstractDayparting{ //Composite
}
class TimePart extends AbstractDayparting{ // Leaf
}

enter image description here

I feel i can use the composite pattern, but i have two bad feelings:

  1. My custom composite pattern is pretty "rigid" because the leafs are always in the lowest level.
  2. I need to get objects from a particular level.

I don't know if (1) is fine, and (2) i don't know how to get all the the composite objects of an specific type (for ex: All the DayParts).

I'm forcing the pattern? it's there a better solution to this problem?


Solution

  • My custom composite pattern is pretty "rigid" because the leafs are always in the lowest level.

    one A has many Bs, and one B has many Cs. It's natural C is in the lowest level. Nothing wrong about it.

    I need to get objects from a particular level.

    Since DayPart belongs to DayParting, you call DayParting.getDayParts() to get all DayParts. Example code:

        /**
         * A parting is a period of time.
         */
        abstract class Parting {
          final int beginingHour;
          final int endHour;
          public Parting(int beginingHour, int endHour) {
            this.beginingHour = beginingHour;
            this.endHour = endHour;
          }
    
          public int getBeginingHour() {
            return beginingHour;
          }
    
          public int getEndHour() {
            return endHour;
          }
        }
    
        class DayParting extends Parting {
          List<DayPart> dayParts = new ArrayList<DayPart>();
    
          public DayParting(int beginingHour, int endHour) {
            super(beginingHour, endHour);
          }
    
          public List<DayPart> getDayParts() {
            return dayParts;
          }
        }
    
        class DayPart extends Parting {
          List<TimePart> timeParts= new ArrayList<TimePart>();
    
          public DayPart(int beginingHour, int endHour) {
            super(beginingHour, endHour);
          }
        }
    
        class TimePart extends Parting {
          public TimePart(int beginingHour) {
            super(beginingHour, beginingHour + 1); // only one hour
          }
        }
    

    Assume all DayParts belonging to DayParting has the same (beginingHour,endHour), You add this

        class DayParting extends Parting {
          public void addDayPart() {
            dayParts.add(new DayPart(this.beginingHour, this.endHour));
          }
        }
    

    Assume each DayPart belonging to DayParting has indivisual (beginingHour,endHour), You add this

        class DayParting extends Parting {
          public void addDayPart(int beginingHour, int endHour) {
            dayParts.add(new DayPart(beginingHour, endHour));
          }
        }
    

    The thing is, DayParting has only one DayPart type, but many instances, so it's still Composite pattern. The good thing is, you can add more type in the future, with very small change.

        class VariantDayPart extends DayPart {
          public VariantDayPart(int beginingHour, int endHour) {
            super(beginingHour, endHour);
          }
        }