mardi 26 mai 2020

Design Patterns and OOP principle in Java

Question to solve:

"There are several types of accounting service Orders. The current solution for these orders is to create a new class for each type (based on work type e.g. audits or day-to-day work, whether the order is for priority client , and whether the order is a one off or regularly scheduled work). The full system has 66 * 2 * 2 of these classes (264 order classes!), with 8 of these (2*2*2) provided to you as an example – CPA would like you to find a way to reduce this class load without breaking the existing Order interface."

The Code related to the question:

 if (isScheduled) {
  if (1 == orderType) { 
    if (isCritical) {
      order =
          new FirstOrderTypeScheduled(
              id, clientID, date, criticalLoading, maxCountedEmployees, numQuarters);
    } else {
      order = new Order66Scheduled(id, clientID, date, maxCountedEmployees, numQuarters);
    }
  } else if (2 == orderType) { // 2 is audit
    if (isCritical) {
      order = new CriticalAuditOrderScheduled(id, clientID, date, criticalLoading, numQuarters);
    } else {
      order = new NewOrderImplScheduled(id, clientID, date, numQuarters);
    }
  } else {
    return null;
  }
} else {
  if (1 == orderType) {
    if (isCritical) {
      order = new FirstOrderType(id, clientID, date, criticalLoading, maxCountedEmployees);
    } else {
      order = new Order66(id, clientID, date, maxCountedEmployees);
    }
  } else if (2 == orderType) {
    if (isCritical) {
      order = new CriticalAuditOrder(id, clientID, date, criticalLoading);
    } else {
      order = new NewOrderImpl(id, clientID, date);
    }
  } else {
    return null;
  }
}

So, there are a few types of classes given (8 classes) which are indicated in the above question. What I want to ask is, amongst the design principles and OO design principles (SOLID), is there a method that I should apply in order to solve the above question? (I am thinking of using Bridge Pattern)

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire