Where do you put your dependencies?
Posted
by
The All Foo
on Stack Overflow
See other posts from Stack Overflow
or by The All Foo
Published on 2012-04-14T22:19:59Z
Indexed on
2012/04/14
23:30 UTC
Read the original article
Hit count: 217
If I use the dependency injection pattern to remove dependencies they end up some where else.
For example, Snippet 1, or what I call Object Maker.
I mean you have to instantiate your objects somewhere...so when you move dependency out of one object, you end up putting it another one.
I see that this consolidates all my dependencies into one object. Is that the point, to reduce your dependencies so that they all reside in a single ( as close to as possible ) location?
Snippet 1 - Object Maker
<?php
class ObjectMaker
{
public function makeSignUp()
{
$DatabaseObject = new Database();
$TextObject = new Text();
$MessageObject = new Message();
$SignUpObject = new ControlSignUp();
$SignUpObject->setObjects($DatabaseObject, $TextObject, $MessageObject);
return $SignUpObject;
}
public function makeSignIn()
{
$DatabaseObject = new Database();
$TextObject = new Text();
$MessageObject = new Message();
$SignInObject = new ControlSignIn();
$SignInObject->setObjects($DatabaseObject, $TextObject, $MessageObject);
return $SignInObject;
}
public function makeTweet( $DatabaseObject = NULL, $TextObject = NULL, $MessageObject = NULL )
{
if( $DatabaseObject == 'small' )
{
$DatabaseObject = new Database();
}
else if( $DatabaseObject == NULL )
{
$DatabaseObject = new Database();
$TextObject = new Text();
$MessageObject = new Message();
}
$TweetObject = new ControlTweet();
$TweetObject->setObjects($DatabaseObject, $TextObject, $MessageObject);
return $TweetObject;
}
public function makeBookmark( $DatabaseObject = NULL, $TextObject = NULL, $MessageObject = NULL )
{
if( $DatabaseObject == 'small' )
{
$DatabaseObject = new Database();
}
else if( $DatabaseObject == NULL )
{
$DatabaseObject = new Database();
$TextObject = new Text();
$MessageObject = new Message();
}
$BookmarkObject = new ControlBookmark();
$BookmarkObject->setObjects($DatabaseObject,$TextObject,$MessageObject);
return $BookmarkObject;
}
}
© Stack Overflow or respective owner