8. CONSTRUCTION BY HAND
- (id)initWithSpellChecker:(id<SpellChecker>) spellChecker;
•
Нет сокрытия зависимостей
•
“Тестируемо”
•
Инъекция ‘руками’, нет
возможности сменить все и сразу
• Клиенты сервисов должны знать
как связаны графы объектов
- (id)init;
@__smirnov__
11. @interface EmailerFactory{}
- (id<Emailer>) createKlingonEmailer;
@end
FACTORY PATTERN
• Нет сокрытия зависимостей
• “Тестируемо”
• Есть способ сменить все
зависимости разом
• Сами создаем объекты
• Больше сложных зависимостей
это более сложные фабрики
• Меняем код, чтобы тестировать
• По фабрике на каждый сервис ?
@__smirnov__
13. SERVICE LOCATOR PATTERN
The key difference is that with a Service Locator every user of a
service has a dependency to the locator.The locator can hide
dependencies to other implementations, but you do need to see
the locator. (c) Martin Fowler
So the decision between locator and injector depends on whether that
dependency is a problem.
@__smirnov__
18. CONSTRUCTOR INJECTION
Полностью готовый к работе объект
@interface SMTPEmailer<Emailer>
- (instancetype) initWithSpellchecker:(id<Spellchecker>) spellchecker
addressbook:(id<AddressBook>) addressbook;
@end
@interface EmailerClient: NSObject
- (instancetype) initWithEmailer:(id<Emailer>) emailer;
@end
@__smirnov__
19. SETTER INJECTION
@interface SMTPEmailer<Emailer>
@property(nonatomic, strong) id <Spellchecker> spellchecker;
@property(nonatomic, strong) id <AddressBook> addressbook;
@end
Можем модифицировать зависимости
Можем получить полусобранный объект
Можем намеренно не предоставлять часть зависимостей
@__smirnov__
20. METHOD INJECTION (AOP)
- (void) sendLetterWithSubject:(NSString*) subject
text:(NSString*) text {
NSLog(@"enter sendLetterWithSubject");
// do some real work
NSLog(@"exit sendLetterWithSubject");
}
@__smirnov__
25. ALWAYS ALMOST GUICE
•“Annotation” Based Dependency
Injection
•Custom Object Providers
•Protocol Bindings
•Instance Bindings
•Lazily instantiates dependencies
Objection orTyphoon
•All above (except “annotation”)+
•!Autowiring!
•Assembling with blocks or XML
•AppCode integration
@__smirnov__
26. MATERIALS USED
• Inversion of Control Containers and the Dependency Injection
pattern by Martin Fowler
• Dependency Injection by Dhanji R. Prasanna ( author of Guice )
• Design Patterns: Dependency Injection by Griffin Caprio
( MSDN Magazine, September 2005 )
• Big Modular Java with Guice ( Google I/O 2009 )
• Dependency Injection ( !Wikipedia! )
@__smirnov__