<?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: To &#8220;new&#8221; or not to &#8220;new&#8221;&#8230;</title>
	<atom:link href="http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/feed/" rel="self" type="application/rss+xml" />
	<link>http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=to-new-or-not-to-new</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: Lars-Erik Roald</title>
		<link>http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/comment-page-1/#comment-10383</link>
		<dc:creator>Lars-Erik Roald</dc:creator>
		<pubDate>Thu, 15 Sep 2011 06:28:29 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=246#comment-10383</guid>
		<description>Have you considered the factory pattern ?
And letting having an ISongSetup interface that is only visible to the SongFactory ? The factory itself just resolves directly from the IOC container by calling some Resolve method and then call Setup(arg..) on the Song.

(eg 
Song : ISongSetup
ISongSetup : ISong
ISongFactory)

If you dont like exposing the IOC Container to the factory, just create your own wrapper around the container.</description>
		<content:encoded><![CDATA[<p>Have you considered the factory pattern ?<br />
And letting having an ISongSetup interface that is only visible to the SongFactory ? The factory itself just resolves directly from the IOC container by calling some Resolve method and then call Setup(arg..) on the Song.</p>
<p>(eg<br />
Song : ISongSetup<br />
ISongSetup : ISong<br />
ISongFactory)</p>
<p>If you dont like exposing the IOC Container to the factory, just create your own wrapper around the container.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/comment-page-1/#comment-7505</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Mon, 04 Apr 2011 04:01:18 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=246#comment-7505</guid>
		<description>@David,

perhaps you could split it into two objects.  SongInfo which is newable, and a Song which is also newable but which you get asking the repository

Song s = songRepository.get(new SongInfo());

This way you have a clear separation between the object which you can create and which needs to be populated by the system.</description>
		<content:encoded><![CDATA[<p>@David,</p>
<p>perhaps you could split it into two objects.  SongInfo which is newable, and a Song which is also newable but which you get asking the repository</p>
<p>Song s = songRepository.get(new SongInfo());</p>
<p>This way you have a clear separation between the object which you can create and which needs to be populated by the system.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bob</title>
		<link>http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/comment-page-1/#comment-7501</link>
		<dc:creator>Bob</dc:creator>
		<pubDate>Sun, 03 Apr 2011 23:49:03 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=246#comment-7501</guid>
		<description>@David,

I believe that that injectable option that Misko is describing isn&#039;t to make the Song an injectable but is instead to inject an injectable (in the form of a Song factory) that will then create the Song.</description>
		<content:encoded><![CDATA[<p>@David,</p>
<p>I believe that that injectable option that Misko is describing isn&#8217;t to make the Song an injectable but is instead to inject an injectable (in the form of a Song factory) that will then create the Song.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David</title>
		<link>http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/comment-page-1/#comment-7480</link>
		<dc:creator>David</dc:creator>
		<pubDate>Sat, 02 Apr 2011 12:21:15 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=246#comment-7480</guid>
		<description>The Dark Side: I will definitely steer clear of global variables and singletons (and static calls if possible). I&#039;ve learned it the hard way in my tests, that what at first seems like a shortcut is in fact a very painful road to hell.

The first option (pass the loader into each method) does not appeal to me much for the stated reasons. But I think that the other approach (inject the loader into Song) is flawed as well. Please correct me if I am wrong:

a) The Song is an Injectable now, but it has state - Seems wrong to me. Is it ok?

b) The Song does not fit to any DDD category now - The Song class with the Loader field reference is neither a service (it has state and overall it does not look like a service) nor an entity (or is it?). Will this mixed nature of the Song class backfire at some point?

c) We are breaking our ground rules here - &quot;...Injectable can never ask for a non-Injectable (Newable) in its constructor...&quot;. When the Song was a Newable, we could easily pass primitives or other newables to its constructor to initialize it. But this should be a &#039;NO NO&#039; for Injectables.

All the three points seem to be intertwined and maybe it&#039;s just three guises of the same issue. Anyway I am baffled. (I hope you say that my conclusions are wrong. Otherwise it would seem that the newable/injectable scheme cannot be fitted to a very simple model?)</description>
		<content:encoded><![CDATA[<p>The Dark Side: I will definitely steer clear of global variables and singletons (and static calls if possible). I&#8217;ve learned it the hard way in my tests, that what at first seems like a shortcut is in fact a very painful road to hell.</p>
<p>The first option (pass the loader into each method) does not appeal to me much for the stated reasons. But I think that the other approach (inject the loader into Song) is flawed as well. Please correct me if I am wrong:</p>
<p>a) The Song is an Injectable now, but it has state &#8211; Seems wrong to me. Is it ok?</p>
<p>b) The Song does not fit to any DDD category now &#8211; The Song class with the Loader field reference is neither a service (it has state and overall it does not look like a service) nor an entity (or is it?). Will this mixed nature of the Song class backfire at some point?</p>
<p>c) We are breaking our ground rules here &#8211; &#8220;&#8230;Injectable can never ask for a non-Injectable (Newable) in its constructor&#8230;&#8221;. When the Song was a Newable, we could easily pass primitives or other newables to its constructor to initialize it. But this should be a &#8216;NO NO&#8217; for Injectables.</p>
<p>All the three points seem to be intertwined and maybe it&#8217;s just three guises of the same issue. Anyway I am baffled. (I hope you say that my conclusions are wrong. Otherwise it would seem that the newable/injectable scheme cannot be fitted to a very simple model?)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/comment-page-1/#comment-7470</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Fri, 01 Apr 2011 16:59:41 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=246#comment-7470</guid>
		<description>@David,

Your analysis of the problem is very good. What you are asking for is not possible, since new-ables can not have a field reference to an injectable. You have two choices

1) pass the loading service into each method. (That seems like a bad idea, and puts too much work on the user of your class, and probably breaks Law of Demeter)
2) change the object from newable to injectable. So the user will say songFactory.create() rather then new Song(). The factory can then set the field. 

There really are no other options, unless you go to the dark side and create a global variable, which is always a bad idea.</description>
		<content:encoded><![CDATA[<p>@David,</p>
<p>Your analysis of the problem is very good. What you are asking for is not possible, since new-ables can not have a field reference to an injectable. You have two choices</p>
<p>1) pass the loading service into each method. (That seems like a bad idea, and puts too much work on the user of your class, and probably breaks Law of Demeter)<br />
2) change the object from newable to injectable. So the user will say songFactory.create() rather then new Song(). The factory can then set the field. </p>
<p>There really are no other options, unless you go to the dark side and create a global variable, which is always a bad idea.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David</title>
		<link>http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/comment-page-1/#comment-7468</link>
		<dc:creator>David</dc:creator>
		<pubDate>Fri, 01 Apr 2011 09:58:51 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=246#comment-7468</guid>
		<description>Hi Misko, I cannot get my head around clear implementation of lazy loading while adhering to the newable/injectable paradigm. I want my DDD entities to implement transparent lazy loading and to be true newables at the same time. How to achieve this?

Let&#039;s continue in your example and take the Song as an entity. Song has member properties Song.id (unique song id), Song.loaded (boolean) which indicates if the song has been loaded (e.g. from DB or disk). I also have a field reference to Loader object which does the actual loading (I know this is bad). And finally there are class members for other song properties: song data, author, year, etc.

Every setter and getter first checks if the object has been loaded and if not it calls this.loader.load(this). This way I have lazy loading transparent to the client and I can use very &#039;light&#039; Song objects in my system, which get loaded only when needed. Until then they are just empty shells with only id set.

Now I want to factor out the Loader service object (injectable) from the Song (newable) and keep the lazy loading transparent to the client (so that client can call e.g. song.getAuthor() as usual). And I am stuck here. How to invoke the loader service object from getters and setters and not keep a field reference to it in Song? It would be possible to pass the loader to every getter and setter (string Song.getAuthor(Loader loader)) but it seems a lot of hassle and it complicates things for the client.

Is there a better way? Or am I missing something completely? Thanks for any hint.

Note 1: Sorry for my terminology and notation. I am a PHP guy and I am just guessing the Java equivalents.

Note 2: This blog is excellent! Thanks Misko. I wish this kind of knowledge was available also in the PHP world.</description>
		<content:encoded><![CDATA[<p>Hi Misko, I cannot get my head around clear implementation of lazy loading while adhering to the newable/injectable paradigm. I want my DDD entities to implement transparent lazy loading and to be true newables at the same time. How to achieve this?</p>
<p>Let&#8217;s continue in your example and take the Song as an entity. Song has member properties Song.id (unique song id), Song.loaded (boolean) which indicates if the song has been loaded (e.g. from DB or disk). I also have a field reference to Loader object which does the actual loading (I know this is bad). And finally there are class members for other song properties: song data, author, year, etc.</p>
<p>Every setter and getter first checks if the object has been loaded and if not it calls this.loader.load(this). This way I have lazy loading transparent to the client and I can use very &#8216;light&#8217; Song objects in my system, which get loaded only when needed. Until then they are just empty shells with only id set.</p>
<p>Now I want to factor out the Loader service object (injectable) from the Song (newable) and keep the lazy loading transparent to the client (so that client can call e.g. song.getAuthor() as usual). And I am stuck here. How to invoke the loader service object from getters and setters and not keep a field reference to it in Song? It would be possible to pass the loader to every getter and setter (string Song.getAuthor(Loader loader)) but it seems a lot of hassle and it complicates things for the client.</p>
<p>Is there a better way? Or am I missing something completely? Thanks for any hint.</p>
<p>Note 1: Sorry for my terminology and notation. I am a PHP guy and I am just guessing the Java equivalents.</p>
<p>Note 2: This blog is excellent! Thanks Misko. I wish this kind of knowledge was available also in the PHP world.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/comment-page-1/#comment-7369</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Fri, 25 Mar 2011 15:45:58 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=246#comment-7369</guid>
		<description>@Steven,

I am not a C++ expert, but what you are proposing sounds like the right thing to do. I think you are on the right track.</description>
		<content:encoded><![CDATA[<p>@Steven,</p>
<p>I am not a C++ expert, but what you are proposing sounds like the right thing to do. I think you are on the right track.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steven</title>
		<link>http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/comment-page-1/#comment-7368</link>
		<dc:creator>Steven</dc:creator>
		<pubDate>Fri, 25 Mar 2011 14:00:07 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=246#comment-7368</guid>
		<description>RE: Applying the &#039;CleanCodeTalks&#039; guidelines to C++ code.


I&#039;m a big fan of the CleanCodeTalks presentations and follow their guidelines when I&#039;m writing Java code.

I&#039;d like to apply these same guidelines when writing C++ code.

In particular I&#039;m looking to understand the best way to follow the dependency-injection and testability guidelines in order to:

a.  structure code to have object construction kept inside factories,

b.  keep calls to &#039;new&#039; out of constructors,

c.  make it obvious to any user of a class
   what the dependencies of the class are
   just by the user looking at the parameter list
   of the constructor.


My question is what is the best way to design a C++ class which has a field that has a &quot;complex-type&quot;, as the constructor for my class will have to invoke the constructor for the field.

I&#039;ve reviewed the guidelines and here&#039;s what I&#039;m thinking.
To follow these guidelines I should:

1. replace fields that have &quot;complex-type&quot; with a &quot;pointer-to-complex-type&quot;

2. in the constructor declare a parameter of type &quot;pointer-to-complex-type&quot;.

3. in the factory use &#039;new&#039; to construct an object of type &quot;pointer-to-complex-type&quot;
   and pass that to the constructor of &quot;my-class&quot;.

[  IIRC, I think the guidelines recommended that if the field had a &quot;complex-type&quot; that was a collection type  - like std::vector - then there&#039;s no need to construct it externally from &quot;my-class&quot; constructor.  ]

I assume that as a general rule it wouldn&#039;t be good design to keep the field as a &quot;complex-type&quot; and have the constructor declare a parameter of &quot;ref-to-complex-type&quot; as the copy-constructor for the &quot;complex-type&quot; is likely to be too expensive or possibly prohibited by the &quot;complex-type&quot; declaring it private.


As an example, for a &#039;MyFileLogger&#039; class that needs to access a &#039;std::ofstream&#039; object, if I follow the guidelines (a, b, c) then the &#039;MyFileLogger&#039; class that I might have written as:

    class MyFileLogger
    {
        std::ofstream theFileStr;
        ....
    public: MyFileLogger(const char* aFilename) : theFileStr(aFilename) {}
    ....
    };

I&#039;m guessing that I should instead write it as:

    class MyFileLogger
    {
        std::ofstream* theFileStr;
        ...
    public: MyFileLogger(std::oftream* aFileStr) : theFileStr(aFileStr) {}
    ...
    };

and construct it in the factory method:

    const char* theFilename = &quot;logfile.txt&quot;;

    // might be wise to wrap the &#039;std::oftream*&#039; in a smart-pointer
    // object to manage it&#039;s destruction properly.
    //
    std::ofstream* aFileStr = new std::ofstream(theFilename);

    MyFileLogger theFileLogger(aFileStr);



I would welcome your advice on whether this is the right approach.


Cheers,


Steven</description>
		<content:encoded><![CDATA[<p>RE: Applying the &#8216;CleanCodeTalks&#8217; guidelines to C++ code.</p>
<p>I&#8217;m a big fan of the CleanCodeTalks presentations and follow their guidelines when I&#8217;m writing Java code.</p>
<p>I&#8217;d like to apply these same guidelines when writing C++ code.</p>
<p>In particular I&#8217;m looking to understand the best way to follow the dependency-injection and testability guidelines in order to:</p>
<p>a.  structure code to have object construction kept inside factories,</p>
<p>b.  keep calls to &#8216;new&#8217; out of constructors,</p>
<p>c.  make it obvious to any user of a class<br />
   what the dependencies of the class are<br />
   just by the user looking at the parameter list<br />
   of the constructor.</p>
<p>My question is what is the best way to design a C++ class which has a field that has a &#8220;complex-type&#8221;, as the constructor for my class will have to invoke the constructor for the field.</p>
<p>I&#8217;ve reviewed the guidelines and here&#8217;s what I&#8217;m thinking.<br />
To follow these guidelines I should:</p>
<p>1. replace fields that have &#8220;complex-type&#8221; with a &#8220;pointer-to-complex-type&#8221;</p>
<p>2. in the constructor declare a parameter of type &#8220;pointer-to-complex-type&#8221;.</p>
<p>3. in the factory use &#8216;new&#8217; to construct an object of type &#8220;pointer-to-complex-type&#8221;<br />
   and pass that to the constructor of &#8220;my-class&#8221;.</p>
<p>[  IIRC, I think the guidelines recommended that if the field had a "complex-type" that was a collection type  - like std::vector - then there's no need to construct it externally from "my-class" constructor.  ]</p>
<p>I assume that as a general rule it wouldn&#8217;t be good design to keep the field as a &#8220;complex-type&#8221; and have the constructor declare a parameter of &#8220;ref-to-complex-type&#8221; as the copy-constructor for the &#8220;complex-type&#8221; is likely to be too expensive or possibly prohibited by the &#8220;complex-type&#8221; declaring it private.</p>
<p>As an example, for a &#8216;MyFileLogger&#8217; class that needs to access a &#8216;std::ofstream&#8217; object, if I follow the guidelines (a, b, c) then the &#8216;MyFileLogger&#8217; class that I might have written as:</p>
<p>    class MyFileLogger<br />
    {<br />
        std::ofstream theFileStr;<br />
        &#8230;.<br />
    public: MyFileLogger(const char* aFilename) : theFileStr(aFilename) {}<br />
    &#8230;.<br />
    };</p>
<p>I&#8217;m guessing that I should instead write it as:</p>
<p>    class MyFileLogger<br />
    {<br />
        std::ofstream* theFileStr;<br />
        &#8230;<br />
    public: MyFileLogger(std::oftream* aFileStr) : theFileStr(aFileStr) {}<br />
    &#8230;<br />
    };</p>
<p>and construct it in the factory method:</p>
<p>    const char* theFilename = &#8220;logfile.txt&#8221;;</p>
<p>    // might be wise to wrap the &#8216;std::oftream*&#8217; in a smart-pointer<br />
    // object to manage it&#8217;s destruction properly.<br />
    //<br />
    std::ofstream* aFileStr = new std::ofstream(theFilename);</p>
<p>    MyFileLogger theFileLogger(aFileStr);</p>
<p>I would welcome your advice on whether this is the right approach.</p>
<p>Cheers,</p>
<p>Steven</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/comment-page-1/#comment-3046</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Mon, 08 Feb 2010 02:56:29 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=246#comment-3046</guid>
		<description>@ Juraj,

I would solve this in this way

interface Output;
class Speaker implements Output;
class Headset implements Output;
class OutputSelector(Speaker, Headset) implements Output;
class MusicPlayer(Output);

s = new Speaker()
h = new Headset()
os = new OutputSerector(s, h);
mp = new MusicPlayer(os);

os.seletOutput(...);
wp.play();</description>
		<content:encoded><![CDATA[<p>@ Juraj,</p>
<p>I would solve this in this way</p>
<p>interface Output;<br />
class Speaker implements Output;<br />
class Headset implements Output;<br />
class OutputSelector(Speaker, Headset) implements Output;<br />
class MusicPlayer(Output);</p>
<p>s = new Speaker()<br />
h = new Headset()<br />
os = new OutputSerector(s, h);<br />
mp = new MusicPlayer(os);</p>
<p>os.seletOutput(&#8230;);<br />
wp.play();</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Juraj Blahunka</title>
		<link>http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/comment-page-1/#comment-3042</link>
		<dc:creator>Juraj Blahunka</dc:creator>
		<pubDate>Sun, 07 Feb 2010 00:15:55 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=246#comment-3042</guid>
		<description>@Misko What about injectables, that are required for object instantiation but can be changed with another values? (like your MusicPlayer, which needs AudioDevice)... Sure AudioDevice is needed in MusicPlayer&#039;s constructor, but AudioDevice can change (from Speakers to Headset).. Should a SETTER be introduced? Doesn&#039;t this bring ambiguity to the MusicPlayer class?</description>
		<content:encoded><![CDATA[<p>@Misko What about injectables, that are required for object instantiation but can be changed with another values? (like your MusicPlayer, which needs AudioDevice)&#8230; Sure AudioDevice is needed in MusicPlayer&#8217;s constructor, but AudioDevice can change (from Speakers to Headset).. Should a SETTER be introduced? Doesn&#8217;t this bring ambiguity to the MusicPlayer class?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

