Business rules - where do they go to OOP?

I have a class: Schedule.

public class Schedule {

private int locationNum;
private int cost;
private String costReason; 
private Date weekOfChange;
private Date dayOfChange;
private String changeReason; 

// and all those getters and setters

public Schedule(int locationNum, int cost, String costReason, Date weekOfChange, Date dayOfChange, String changeReason) throws ApplicationException {
//change is all or nothing - all attributes are present or none
if((weekOfChange!=null && dayOfChange!=null && changeReason!=null) || (weekOfChange==null  && dayOfChange == null && changeReason == null))  {
this.weekOfChange = weekOfChange;
this.dayOfChange = dayOfChange;
this.changeReason = changeReason;
}
else { throw new ApplicationException();}
//similary another if block to ensure that if cost is specified 
//then there exists the corresponding reason code for it.
}
}

So far, I have enjoyed my Schedule class. However, I did not finish the check, I would have to do some other checks:

  • - This locationNum is a valid storage number in the database.
  • is changeReason is the text of one of these 6 different changeReason codes in the database.
  • etc. etc.

As a rule, I would not write them in the Schedule class, obviously, I can not call DAO from this class. Thus, I would have a business layer and some kind of validator class that accepts an object of type Schedule and performs a sequence of database checks in sequence and collects errors for display / independently.

Now here are my questions:

  • Schedule POJO , - -. , , ? , ?
  • , , , -, -. , - dayOfChange NULL , . , , , ? coz , , . , , , , , Schedule.
  • ? ?

.

+3
5

" ", , . , weekOfChange, dayOfChange changeReason , . , , ?

, , , . . , . (, weekOfChange, dayOfChannge changeReason, Schedule. "" .

(, ). . , Schedule , .

, : , . "", .

,

    class Schedule {
         private Change change;
         private Cost cost;
         private Location location;

        public Schedule(Location location, Cost cost, Change change) {
           this.change = change;
           this.cost = cost;
           this.location = location;
        }
}

colloborators

public class Change {
    private Date weekOfChange; //shoudln't these two fields be one, a date is a date after all??
    private Date dayOfChange; //shoudln't these two fields be one??
    private String changeReason; 

    public Change(Date weekOfChange Date dayOfChange, String changeReason) {
      this.weekOfChange = weekOfChange;
      ...etc.
    } 
}

, .

+5

POJO , - . , ? , ?

POJO , , OR . , - .

, , , ? coz , . , , , , , , , .

, , , Schedule? , private-private - .

? ?

, , , .

:

  • , Schedule .
  • .
  • , ( ).
+4

, Schedule. . .

locationNum changeReason. . , . . Schedule . Schedule , locationNum changeReason . , Schedule, locationNum changeReason , , . - ( friend) .

POJO ( ) , , . , "". .

-, , . , , -, . - ().

, () . , . , , .

. .

+2

locationNum int, , Location? , "" ORM ( ) .

+1

:

1) , , DTO , .

2) -, , , - , , - InvalidDataException - 't "" . ( ), business/dao .

0

Source: https://habr.com/ru/post/1709750/


All Articles