Encapsulating a service call within a domain object object

Is this a valid object design? I have a domain object where I insert the service and call the verification method to update the state of the object, and if everything goes well, send a confirmation message. The code looks like this:

class Foo {
  String bar
  Service emailService


  public boolean verify() {
    bar = "foo"
        if(this.save()) {
            emailService.sendConfirmation()
        }
  }
}

Foo.get(1).verify()

Is it allowed to call the email service in this case? Is there a design template that I can use for such a situation.

Thank. - Ken

+5
source share
3 answers

. , . , - , .

- , , ( ).

, - .

' ( , ) . . :

class Foo {
  String bar    

  public boolean verify(Service emailService) {
    bar = "foo"
        if(this.save()) {
            emailService.sendConfirmation()
        }
  }
}

Foo.get(1).verify(new Service(...))

( ) . Udi Dahan. , . .

,

+10

, . , .

, . .

+3

(?), . , , , Foo ConfirmationMessage ( ), . .

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

+2

All Articles