Skip to content

Instantly share code, notes, and snippets.

@up1
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save up1/baf1c912123b569fd1c0 to your computer and use it in GitHub Desktop.

Select an option

Save up1/baf1c912123b569fd1c0 to your computer and use it in GitHub Desktop.
Demo :: DI
public class Email {
public void send() {
}
}
public class Email implements MessageService {
public void send() {
}
}
public interface MessageService {
public void send();
}
public class Notification {
Email email;
public Notification() {
email = new Email();
}
public void notifyPromotion() {
email.send();
}
}
public class Notification {
MessageService messageService;
public Notification() {
messageService = new Email();
}
public void notifyPromotion() {
messageService.send();
}
}
public class Notification {
private MessageService messageService;
public Notification(MessageService messageService) {
this.messageService = messageService;
}
public void notifyPromotion() {
messageService.send();
}
}
public class Notification {
private MessageService messageService;
public void setMessageService(MessageService messageService) {
this.messageService = messageService;
}
public void notifyPromotion() {
if (this.messageService != null) {
messageService.send();
}
}
}
public class Notification {
public void notifyPromotion(MessageService messageService) {
messageService.send();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment