<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Circular Dependency in constructors and Dependency Injection</title>
	<atom:link href="http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/feed/" rel="self" type="application/rss+xml" />
	<link>http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=circular-dependency-in-constructors-and-dependency-injection</link>
	<description>Testability Explorer</description>
	<lastBuildDate>Thu, 19 Jan 2012 16:42:57 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Matt</title>
		<link>http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/comment-page-1/#comment-9876</link>
		<dc:creator>Matt</dc:creator>
		<pubDate>Tue, 16 Aug 2011 14:27:06 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=146#comment-9876</guid>
		<description>How about something like listener and provider interfaces?

I could have a class that has a port connection and is able to provide to a single listener. However the thing listening also wants to be able to send things out on the port so needs a reference to the port (whether directly or via an interface)

interface Listener
{
void heardSomething(String thing);
}

class Port
{
Port(Listener listener);
void write(String stuff);
}

class ClassA implements Listener
{
ClassA(Port writeTo);
}

Now when it comes to creating this, I need to be able to pass ClassA to Port (as an interface) and Port to ClassA as follows:

main
{
ClassA a = new ClassA(Port...?)
Port p = new Port(a);
}

Now I know that this could be loosened up by allowing more than one listener and rather than passing in the constructor using an AddListener approach, but I want to see how you would work this with the 3rd class approach.

Matt</description>
		<content:encoded><![CDATA[<p>How about something like listener and provider interfaces?</p>
<p>I could have a class that has a port connection and is able to provide to a single listener. However the thing listening also wants to be able to send things out on the port so needs a reference to the port (whether directly or via an interface)</p>
<p>interface Listener<br />
{<br />
void heardSomething(String thing);<br />
}</p>
<p>class Port<br />
{<br />
Port(Listener listener);<br />
void write(String stuff);<br />
}</p>
<p>class ClassA implements Listener<br />
{<br />
ClassA(Port writeTo);<br />
}</p>
<p>Now when it comes to creating this, I need to be able to pass ClassA to Port (as an interface) and Port to ClassA as follows:</p>
<p>main<br />
{<br />
ClassA a = new ClassA(Port&#8230;?)<br />
Port p = new Port(a);<br />
}</p>
<p>Now I know that this could be loosened up by allowing more than one listener and rather than passing in the constructor using an AddListener approach, but I want to see how you would work this with the 3rd class approach.</p>
<p>Matt</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Oleg</title>
		<link>http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/comment-page-1/#comment-3558</link>
		<dc:creator>Oleg</dc:creator>
		<pubDate>Mon, 31 May 2010 20:15:30 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=146#comment-3558</guid>
		<description>Hi, Spencer.

The relation is not like this. Actually you have this:
Teacher{
  List teachedCourses
}

Course {
  Teacher teacher
  List attendingStudents
}

Student {
  List myCourses
}

So t -&gt; s and s -&gt; t are derived relations. You wouldn&#039;t pass S to T nor T to S in the constructor.
In the next step I would dispute bidirectional references above...

Oleg</description>
		<content:encoded><![CDATA[<p>Hi, Spencer.</p>
<p>The relation is not like this. Actually you have this:<br />
Teacher{<br />
  List teachedCourses<br />
}</p>
<p>Course {<br />
  Teacher teacher<br />
  List attendingStudents<br />
}</p>
<p>Student {<br />
  List myCourses<br />
}</p>
<p>So t -&gt; s and s -&gt; t are derived relations. You wouldn&#8217;t pass S to T nor T to S in the constructor.<br />
In the next step I would dispute bidirectional references above&#8230;</p>
<p>Oleg</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Spencer Hahn</title>
		<link>http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/comment-page-1/#comment-3552</link>
		<dc:creator>Spencer Hahn</dc:creator>
		<pubDate>Mon, 31 May 2010 00:18:28 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=146#comment-3552</guid>
		<description>Say you have two classes, Teacher and Student.
Each student can have multiple teachers &amp; each teacher can have multiple students. The student would need to reference the teacher &amp; the teacher would need to reference the student. so, you would have 
t -&gt; s
t &lt;- s

How would you work around this?</description>
		<content:encoded><![CDATA[<p>Say you have two classes, Teacher and Student.<br />
Each student can have multiple teachers &amp; each teacher can have multiple students. The student would need to reference the teacher &amp; the teacher would need to reference the student. so, you would have<br />
t -&gt; s<br />
t &lt;- s</p>
<p>How would you work around this?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adam Wolff</title>
		<link>http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/comment-page-1/#comment-3408</link>
		<dc:creator>Adam Wolff</dc:creator>
		<pubDate>Tue, 27 Apr 2010 17:02:27 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=146#comment-3408</guid>
		<description>Nice post, misko. I find that this is generally true, though when boot-strapping one system within another, there&#039;s often a circular dependency at the border. For instance, if the engine creates a context in which to load scripts, the root script might need a reference back to the context in order to load more scripts.</description>
		<content:encoded><![CDATA[<p>Nice post, misko. I find that this is generally true, though when boot-strapping one system within another, there&#8217;s often a circular dependency at the border. For instance, if the engine creates a context in which to load scripts, the root script might need a reference back to the context in order to load more scripts.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raghu</title>
		<link>http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/comment-page-1/#comment-3161</link>
		<dc:creator>Raghu</dc:creator>
		<pubDate>Fri, 05 Mar 2010 12:05:48 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=146#comment-3161</guid>
		<description>nice post - came through SO.

I&#039;ve been playing with Apache wicket and ran into this problem. Here&#039;s the scenario:

PageBorder - a border component that wraps around a BasePage and does stuff like setting title, meta etc.

class PageBorder extends Border {
           public PageBorder (id, BasePage page){
                     this.page = page;
                     add (&quot;title&quot;, new Label(&quot;title&quot;, page.getTitle());
           }
}

At the same time, since the BasePage contains the PageBorder, the BasePage needs to know about the border (create the border passing this as a ctor param)
public abstract class BasePage extends Page {
             PageBorder border;
             public add (Component c) {
                       if (border == null) {
                               border = getBorder();
// subclass must create a border passing themselves in
                               add (&quot;border&quot;, border);
                        }
                        border.add(c);
             }
}


In this case, in the BasePage class, I&#039;ve put a protected method - getBorder() - so that the implementing page can decide which border to create.

While this set up works, somehow this seems smelly.

OTOH, if I look at wicket itself, seems like this mutual knowledge is rampant (Page class has a getComponent) and Component classes have a getPage() - however, the coupling is lower.

Any suggestions?</description>
		<content:encoded><![CDATA[<p>nice post &#8211; came through SO.</p>
<p>I&#8217;ve been playing with Apache wicket and ran into this problem. Here&#8217;s the scenario:</p>
<p>PageBorder &#8211; a border component that wraps around a BasePage and does stuff like setting title, meta etc.</p>
<p>class PageBorder extends Border {<br />
           public PageBorder (id, BasePage page){<br />
                     this.page = page;<br />
                     add (&#8220;title&#8221;, new Label(&#8220;title&#8221;, page.getTitle());<br />
           }<br />
}</p>
<p>At the same time, since the BasePage contains the PageBorder, the BasePage needs to know about the border (create the border passing this as a ctor param)<br />
public abstract class BasePage extends Page {<br />
             PageBorder border;<br />
             public add (Component c) {<br />
                       if (border == null) {<br />
                               border = getBorder();<br />
// subclass must create a border passing themselves in<br />
                               add (&#8220;border&#8221;, border);<br />
                        }<br />
                        border.add(c);<br />
             }<br />
}</p>
<p>In this case, in the BasePage class, I&#8217;ve put a protected method &#8211; getBorder() &#8211; so that the implementing page can decide which border to create.</p>
<p>While this set up works, somehow this seems smelly.</p>
<p>OTOH, if I look at wicket itself, seems like this mutual knowledge is rampant (Page class has a getComponent) and Component classes have a getPage() &#8211; however, the coupling is lower.</p>
<p>Any suggestions?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thomas</title>
		<link>http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/comment-page-1/#comment-3074</link>
		<dc:creator>Thomas</dc:creator>
		<pubDate>Sat, 13 Feb 2010 10:56:09 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=146#comment-3074</guid>
		<description>@Misko,

Well, i was trying to solve that problem since a week :D

I&#039;m waiting for new post/presentation about Oriented Object Concept !

Good luck, and thank you !</description>
		<content:encoded><![CDATA[<p>@Misko,</p>
<p>Well, i was trying to solve that problem since a week <img src='http://misko.hevery.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>I&#8217;m waiting for new post/presentation about Oriented Object Concept !</p>
<p>Good luck, and thank you !</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/comment-page-1/#comment-3064</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Fri, 12 Feb 2010 16:48:41 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=146#comment-3064</guid>
		<description>@Thomas, 
Glad you figured it out before I get to respond.</description>
		<content:encoded><![CDATA[<p>@Thomas,<br />
Glad you figured it out before I get to respond.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thomas</title>
		<link>http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/comment-page-1/#comment-3063</link>
		<dc:creator>Thomas</dc:creator>
		<pubDate>Fri, 12 Feb 2010 14:47:14 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=146#comment-3063</guid>
		<description>Hello again :)
Well, I think I have found something ...


class Character {

 private final Client client;
 private final CharStatus status;
 
 // other fields...

 public Character(Client _client, CharStatus _status);

}

public CharStatus {

 private final Client client;
 private int lifePoints;

 public CharStatus(Client _client, int _lifePoint);

 public void fireStatusUpdated(String _property, int _old, int _new); // Using Observer/Observable pattern

}

And finally, class &quot;C&quot; :
class Client {

  public void onStatusUpdate(Client _client, String _property, int _old, int _new);
 
}

The issue was I wanted my collaborator class to send an update to my &quot;super class&quot; Character. Then, my Character send an update to the Client, then Client send data throw network... 

In fact, collaborator classes have to send an update directely to the client...
Circular dependency avoided :)

Sorry for posting !
Thomas.

Ps : I love your job, I think DI is the key for keeping a clean code.
Ps2 : Sorry for ma poor English =) I&#039;m French, and French reputation is to be bad at English ^^</description>
		<content:encoded><![CDATA[<p>Hello again <img src='http://misko.hevery.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
Well, I think I have found something &#8230;</p>
<p>class Character {</p>
<p> private final Client client;<br />
 private final CharStatus status;</p>
<p> // other fields&#8230;</p>
<p> public Character(Client _client, CharStatus _status);</p>
<p>}</p>
<p>public CharStatus {</p>
<p> private final Client client;<br />
 private int lifePoints;</p>
<p> public CharStatus(Client _client, int _lifePoint);</p>
<p> public void fireStatusUpdated(String _property, int _old, int _new); // Using Observer/Observable pattern</p>
<p>}</p>
<p>And finally, class &#8220;C&#8221; :<br />
class Client {</p>
<p>  public void onStatusUpdate(Client _client, String _property, int _old, int _new);</p>
<p>}</p>
<p>The issue was I wanted my collaborator class to send an update to my &#8220;super class&#8221; Character. Then, my Character send an update to the Client, then Client send data throw network&#8230; </p>
<p>In fact, collaborator classes have to send an update directely to the client&#8230;<br />
Circular dependency avoided <img src='http://misko.hevery.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Sorry for posting !<br />
Thomas.</p>
<p>Ps : I love your job, I think DI is the key for keeping a clean code.<br />
Ps2 : Sorry for ma poor English =) I&#8217;m French, and French reputation is to be bad at English ^^</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thomas</title>
		<link>http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/comment-page-1/#comment-3057</link>
		<dc:creator>Thomas</dc:creator>
		<pubDate>Thu, 11 Feb 2010 12:57:12 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=146#comment-3057</guid>
		<description>Hi :)

I have a question about circular dependency...

I have a class Character who is responsible for many and many things : appearance (face, hair...), status (lifepoints...), stats (attack power, defense power...) and i&#039;m stuck !
Look at an example :

class Character {
 private CharAppearance ca;
 private CharStatus status;
 private CharStats stats;

 public Character(CharAppearance _ca, CharStatus _status, CharStats _stats) {
  // 
 }

}

Example of CharStatus :
class CharStatus {

 private int lifePoints;
 private Character owner;
 
 public CharStatus(Character _owner, int _lifePoint) // Here.. circular dependency !

}

How to solve that issue ? Should I remove the owner of CharStatus ? 
Thanks a lot :)
Thomas.</description>
		<content:encoded><![CDATA[<p>Hi <img src='http://misko.hevery.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I have a question about circular dependency&#8230;</p>
<p>I have a class Character who is responsible for many and many things : appearance (face, hair&#8230;), status (lifepoints&#8230;), stats (attack power, defense power&#8230;) and i&#8217;m stuck !<br />
Look at an example :</p>
<p>class Character {<br />
 private CharAppearance ca;<br />
 private CharStatus status;<br />
 private CharStats stats;</p>
<p> public Character(CharAppearance _ca, CharStatus _status, CharStats _stats) {<br />
  //<br />
 }</p>
<p>}</p>
<p>Example of CharStatus :<br />
class CharStatus {</p>
<p> private int lifePoints;<br />
 private Character owner;</p>
<p> public CharStatus(Character _owner, int _lifePoint) // Here.. circular dependency !</p>
<p>}</p>
<p>How to solve that issue ? Should I remove the owner of CharStatus ?<br />
Thanks a lot <img src='http://misko.hevery.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
Thomas.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/comment-page-1/#comment-3039</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Sat, 06 Feb 2010 20:43:14 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=146#comment-3039</guid>
		<description>@Peter,

here is how I would attack you problem.

class Database() implements DB;
class Logger(Database db);
class LoggingDatabase(Logger log, Database db) implements DB;

DB rawDatabase = new Database();
Logger log = new Logger(rowDatabase);
DB dbForTheRestoOfApplication = new LoggingDatabase(log, rowDatabase);

There you go. LoggingDatabase logs the commands as they go by and than delegate it on a class of the same interface. This is know as chain of responsibility and it is my favorite design pattern.</description>
		<content:encoded><![CDATA[<p>@Peter,</p>
<p>here is how I would attack you problem.</p>
<p>class Database() implements DB;<br />
class Logger(Database db);<br />
class LoggingDatabase(Logger log, Database db) implements DB;</p>
<p>DB rawDatabase = new Database();<br />
Logger log = new Logger(rowDatabase);<br />
DB dbForTheRestoOfApplication = new LoggingDatabase(log, rowDatabase);</p>
<p>There you go. LoggingDatabase logs the commands as they go by and than delegate it on a class of the same interface. This is know as chain of responsibility and it is my favorite design pattern.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

