控制反转 (inversion of control)

The inversion of control (IoC) pattern is abstract; it says that one should move dependency creation
out of the consumer class, but it doesn’t talk about exactly how to achieve that.

 

 

public class EmailService
{
public void SendMessage() { ... }
}

 


public
interface IMessagingService { void SendMessage(); } public class EmailService : IMessagingService { public void SendMessage() { ... } } public class NotificationSystem { private IMessagingService svc; public NotificationSystem() { svc = new EmailService(); } public void InterestingEventHappened() { svc.SendMessage(); } }

 

posted on 2015-09-07 11:47  扬扬落叶  阅读(203)  评论(0)    收藏  举报