Why are setters bad at interfaces?

Why do setters work poorly in interfaces if we talk about domain objects?

Clarification:

I have a domain object that is stored in db. It has several fields that are very expensive. I.e.

class JurasicPark {

  private long area;

  ...other fields ...

  getters and setters

  ....

  private Collection<Dinosaur> dinosaurs;
  private Collection<ExoticTree> flora;

  public Collection<Dinosaur> getDinosaurus(){
      ...
  }

  public Collection<ExoticTree> getFlora(){
      ...
  }


}

Fields dinosaursand floravery expensive to initialize and install. But in many cases, I do not need these fields, which need to be set every time.

The problem is that if I go back to the custom instance of the JurasicPark class with dinosaursor flora, don't initialize it to populate the listing with either NPE or some kind of presentation that I throw away. I do not want the api user to think about this and remember which fields may not be set.

, , IJurasicPark IFullJurasicPark, , flora dinosaurs.

   interface IFullJurasicPark extends IJurasicPark

    class IJurasicPark implements IFullJurasicPark

IJurasicPark , , ?

LazyInit.

+3
4

? .

, . . , , .

, , getters + seters . , .

... , . - () upvotes: -)

, / . /, - .

( ), / . , . , " init" Hibernate - IMHO, . , .

, , , JurassicPark . , . , , , , .

, - JurassicPark (), , , , .

+10

, , , . - .

, ( ) , set. .

, . , , Car (, ) int speed /. , , setSpeed .

, setSpeed . , .

private void setSpeed(int newSpeed) {
    if(newSpeed < 0)
        error();
    speed = newSpeed;
}

public void accelerate() {
    setSpeed(getSpeed() + 1);
}

, set , . setSpeed , , .

+1

, , .

, Java, , private/protected ; . ( ), . -, .

+1

, , , , , -. , , , . u, , , .

.

Can someone clarify what I'm saying, or am I mixing things up right now?

what i'm talking about in the code translates as:

Interface IBoard = new ActualBoard();
-1
source

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


All Articles