25
Ivan Mosiev — software architect, AltexSoft [email protected] http://verber.kh.ua @polny_otec

Wake up Neo... Dependencies have you

Embed Size (px)

Citation preview

Page 1: Wake up Neo... Dependencies have you

Ivan Mosiev —

software architect,

AltexSoft

[email protected]

http://verber.kh.ua

@polny_otec

Page 2: Wake up Neo... Dependencies have you

Wake up, Neo...

Dependencies have you

Page 3: Wake up Neo... Dependencies have you

Ideal world

Page 4: Wake up Neo... Dependencies have you

Ideal Unit

Page 5: Wake up Neo... Dependencies have you

Welcome to the real world

Page 6: Wake up Neo... Dependencies have you

Real Unit

Dependencies

Page 7: Wake up Neo... Dependencies have you

Imagine, there is no dependencies

Page 8: Wake up Neo... Dependencies have you

Imaginary Unit

Indirect Input

Indirect Output

Page 9: Wake up Neo... Dependencies have you

Indirect Input

$internal = $Dependency->something;

function dependency() { return $something; }

function dependency() { throw new Exception_WTF; }

function dependency(&$by_ref) { $by_ref++; }

Page 10: Wake up Neo... Dependencies have you

Indirect Output

$Dep->something = $internal;

$Dep->doSomething($internal);

Page 11: Wake up Neo... Dependencies have you

Stub

Page 12: Wake up Neo... Dependencies have you

Dummy

Page 13: Wake up Neo... Dependencies have you

Fake

Page 14: Wake up Neo... Dependencies have you

Spy

Page 15: Wake up Neo... Dependencies have you

Mock

Page 16: Wake up Neo... Dependencies have you

function runMatrix() {

$agents = array();

while (TRUE) {

$agents[] = new AgentSmith();

}

}

This is my world, my world!

Page 17: Wake up Neo... Dependencies have you

Dependency Injection

Page 18: Wake up Neo... Dependencies have you

Refactoring: step 0

function doSomething() {$auth = new Service();

…return $profit;

}

Page 19: Wake up Neo... Dependencies have you

Refactoring: step 1

private function getService() {return new Service();

}

function doSomething() {$service = $this->getService();…return $profit;

}

Page 20: Wake up Neo... Dependencies have you

Refactoring: step 2

private $_service;

private function getService() {If (!$this->_service)

$this->_service = new Service();return $this->_service;

}

function setService(IService $service) {$this->_service = $service;

}

Page 21: Wake up Neo... Dependencies have you

do { $the->same();

} while ($deps_count > 0);

Page 22: Wake up Neo... Dependencies have you

function getService() {

if (!$this->_service)

$this->_service = return Service();

return $this->_service;

}

Page 23: Wake up Neo... Dependencies have you

Inversion of Control

IoC Container store information about dependencies

Dependencies instantiated by Container recursively

Dependencies injected into Object by Service Container

Page 24: Wake up Neo... Dependencies have you

Benefits?

Mock and test everything

Loose coupling

Components reuse

Page 25: Wake up Neo... Dependencies have you

Now you know kung fu