<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.4" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Improving Cairngorm&#8217;s ModelLocator Part 6 - Conclusion</title>
	<link>http://www.bigroom.co.uk/blog/improving-cairngorms-modellocator-part-6/</link>
	<description>Actionscript developer, consultant and troubleshooter</description>
	<pubDate>Fri, 21 Nov 2008 02:15:10 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.4</generator>

	<item>
		<title>by: Theo</title>
		<link>http://www.bigroom.co.uk/blog/improving-cairngorms-modellocator-part-6/#comment-27323</link>
		<pubDate>Tue, 27 May 2008 07:07:57 +0000</pubDate>
		<guid>http://www.bigroom.co.uk/blog/improving-cairngorms-modellocator-part-6/#comment-27323</guid>
					<description>1.

It's not so much semantics as a difference of ideas. I would define a global variable as something that is globally accessible and retains it's state between accesses or invocations, if that is not exactly what a monostate is all about I don't know what. You can call it what you want, but if it acts like a global, it is a global and I don't think that hiding them (or renaming them) makes them less worse.

2.

Cairngorm is a bad architecture. It can be improved, but in the end you would have very little of Cairngorm left. This actually seems like what many do, they start using Cairngorm, realise some of the problems, rip that part out and replace it with something else, notice another problem and rip that out, and so on. In the end they have something they claim to have built using Cairngorm, but in reality it's something &lt;em&gt;built despite Cairngorm&lt;/em&gt;. I criticise Cairngorm quite a lot, and I always get the replies that "oh, no, of course we don't use that part" or "of course we don't use that, but the rest of Cairngorm is just fantastic". What they mean when they say that is that Cairngorm forced them to think about the architecture of their application, made them realise the problems they were having (many because of Cairngorm) and this made their application better. I think that starting off with something that doesn't need to be replaced along the way is a better way of doing it. However, if you learn something from the mistake of using Cairngorm, then you have at least learned something. Just make sure to learn to not make the same mistake twice.

You say that &lt;em&gt;"Cairngorm has the name of Adobe behind it and, like it or not, many developers will use it regardless of any shortcomings it has"&lt;/em&gt;, which is the standard argument for Cairngorm. If you're debating practicalities, then fair enough, you will have it easier find developers who have experience with it and you can slap a nice sticker on the product saying that it was built with Cairngorm and everyone will feel a little bit warm and fuzzy on the inside, &lt;em&gt;it's practical&lt;/em&gt;. However, if it's architecture you want to discuss, then it just doesn't cut it. I criticise Cairngorm because it's an example of bad architecture, and using the "it's backed by Adobe" makes it look like its bad architecture ok -- how bad can it be when it's backed by Adobe? If you want to defend Cairngorm (or global variables for that matter) from an architectural standpoint you have a a lot to prove.

Cairngorm is a little like the Basic family of programming languages: Basic is easy to understand and quick to learn, but you will pick up a lot of bad habits that in the end will make you shoot yourself in the foot once you try to do something more than a simple application. With Cairngorm you may not notice at first, but then the problems will start to show: dependency issues, hard to test, hard to reuse, hard to modularize, etc. Cairngorm takes many of the foundations of object oriented design and do the opposite. Programming to interfaces, why bother? Decoupling, no way. Modularity, fuck it.

I applaud your effort for writing this series, but I really think we need a more open discussion, one where Cairngorm isn't a given, where Cairngorm can be criticised for what it is and where the standard response isn't "but it's backed by Adobe". I realise that that wasn't the intention of this series, but I think it could have been a better series if it wasn't constrained by the architecture and mindset of Cairngorm.</description>
		<content:encoded><![CDATA[<p>1.</p>
<p>It&#8217;s not so much semantics as a difference of ideas. I would define a global variable as something that is globally accessible and retains it&#8217;s state between accesses or invocations, if that is not exactly what a monostate is all about I don&#8217;t know what. You can call it what you want, but if it acts like a global, it is a global and I don&#8217;t think that hiding them (or renaming them) makes them less worse.</p>
<p>2.</p>
<p>Cairngorm is a bad architecture. It can be improved, but in the end you would have very little of Cairngorm left. This actually seems like what many do, they start using Cairngorm, realise some of the problems, rip that part out and replace it with something else, notice another problem and rip that out, and so on. In the end they have something they claim to have built using Cairngorm, but in reality it&#8217;s something <em>built despite Cairngorm</em>. I criticise Cairngorm quite a lot, and I always get the replies that &#8220;oh, no, of course we don&#8217;t use that part&#8221; or &#8220;of course we don&#8217;t use that, but the rest of Cairngorm is just fantastic&#8221;. What they mean when they say that is that Cairngorm forced them to think about the architecture of their application, made them realise the problems they were having (many because of Cairngorm) and this made their application better. I think that starting off with something that doesn&#8217;t need to be replaced along the way is a better way of doing it. However, if you learn something from the mistake of using Cairngorm, then you have at least learned something. Just make sure to learn to not make the same mistake twice.</p>
<p>You say that <em>&#8220;Cairngorm has the name of Adobe behind it and, like it or not, many developers will use it regardless of any shortcomings it has&#8221;</em>, which is the standard argument for Cairngorm. If you&#8217;re debating practicalities, then fair enough, you will have it easier find developers who have experience with it and you can slap a nice sticker on the product saying that it was built with Cairngorm and everyone will feel a little bit warm and fuzzy on the inside, <em>it&#8217;s practical</em>. However, if it&#8217;s architecture you want to discuss, then it just doesn&#8217;t cut it. I criticise Cairngorm because it&#8217;s an example of bad architecture, and using the &#8220;it&#8217;s backed by Adobe&#8221; makes it look like its bad architecture ok &#8212; how bad can it be when it&#8217;s backed by Adobe? If you want to defend Cairngorm (or global variables for that matter) from an architectural standpoint you have a a lot to prove.</p>
<p>Cairngorm is a little like the Basic family of programming languages: Basic is easy to understand and quick to learn, but you will pick up a lot of bad habits that in the end will make you shoot yourself in the foot once you try to do something more than a simple application. With Cairngorm you may not notice at first, but then the problems will start to show: dependency issues, hard to test, hard to reuse, hard to modularize, etc. Cairngorm takes many of the foundations of object oriented design and do the opposite. Programming to interfaces, why bother? Decoupling, no way. Modularity, fuck it.</p>
<p>I applaud your effort for writing this series, but I really think we need a more open discussion, one where Cairngorm isn&#8217;t a given, where Cairngorm can be criticised for what it is and where the standard response isn&#8217;t &#8220;but it&#8217;s backed by Adobe&#8221;. I realise that that wasn&#8217;t the intention of this series, but I think it could have been a better series if it wasn&#8217;t constrained by the architecture and mindset of Cairngorm.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: richard</title>
		<link>http://www.bigroom.co.uk/blog/improving-cairngorms-modellocator-part-6/#comment-27320</link>
		<pubDate>Mon, 26 May 2008 20:27:23 +0000</pubDate>
		<guid>http://www.bigroom.co.uk/blog/improving-cairngorms-modellocator-part-6/#comment-27320</guid>
					<description>&lt;i&gt;I sort of think that we’ve come to the end of this argument, I don’t think I will convince you, and accept that if these ideas work for you then go for them.&lt;/i&gt;

I see only two things we haven't agreed on -

1. Is the state in the Monostate global or not - we don't appear likely to agree on that; you're repeating your arguments and my response would be to repeat mine. The reality is, we probably don't differ by much. I would agree that the static variables in a monostate create dependencies between each instance of the monostate class. But I don't consider these private statics to fall under the definition of "global variable". It's probably just a semantic thing.

2. The second disagreement is harder to define. I think the real disagreement is over whether it's worth trying to improve Cairngorm (and writing about it in a blog) when it's architecture is designed around the idea of classes getting the state they want rather than having the state passed to them. I think it is, because a lot of people use Cairngorm. I don't imagine we'll agree on this either.</description>
		<content:encoded><![CDATA[<p><i>I sort of think that we’ve come to the end of this argument, I don’t think I will convince you, and accept that if these ideas work for you then go for them.</i></p>
<p>I see only two things we haven&#8217;t agreed on -</p>
<p>1. Is the state in the Monostate global or not - we don&#8217;t appear likely to agree on that; you&#8217;re repeating your arguments and my response would be to repeat mine. The reality is, we probably don&#8217;t differ by much. I would agree that the static variables in a monostate create dependencies between each instance of the monostate class. But I don&#8217;t consider these private statics to fall under the definition of &#8220;global variable&#8221;. It&#8217;s probably just a semantic thing.</p>
<p>2. The second disagreement is harder to define. I think the real disagreement is over whether it&#8217;s worth trying to improve Cairngorm (and writing about it in a blog) when it&#8217;s architecture is designed around the idea of classes getting the state they want rather than having the state passed to them. I think it is, because a lot of people use Cairngorm. I don&#8217;t imagine we&#8217;ll agree on this either.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Theo</title>
		<link>http://www.bigroom.co.uk/blog/improving-cairngorms-modellocator-part-6/#comment-27319</link>
		<pubDate>Mon, 26 May 2008 18:57:35 +0000</pubDate>
		<guid>http://www.bigroom.co.uk/blog/improving-cairngorms-modellocator-part-6/#comment-27319</guid>
					<description>I sort of think that we've come to the end of this argument, I don't think I will convince you, and accept that if these ideas work for you then go for them.

However, the thing about Monostate needs to be cleared up:

&lt;em&gt;The monostate uses private static variables. The scoping of these variables is the class itself. They are not accessible from outside this scope and hence are not global.&lt;/em&gt;

No matter how much you hide the global variable (in this case the state of the monostate class, which is kept in static variables, which makes it a collection of global variables) it will remain a global variable.

If you sprinkle your code with &lt;code&gt;new MyMonostate()&lt;/code&gt; instead of &lt;code&gt;MySingleton.getInstance()&lt;/code&gt; you will have tied your application to a concrete implementation and a global variable, regarless of the global variable appearing in disguise as multiple instances. Since all instances have the same state, which is global, they are nothing more than a facade to a collection of global variables.

The monostate may not look like a global variable, and you can pretend that it isn't, but you can't change the design later to allow for multiple instances because your code acts like it was all the same instance. This is exactly the same problem as using a singleton by accessing it with it's &lt;code&gt;getInstance&lt;/code&gt; all over the place.

If your code looks like this:

&lt;code&gt;
var myMonostate = new MyMonostate();

myMonostate.setTheThing(3);

//... snip ...

var myMonostate = new MyMonostate();

var theThing = myMonostate.getTheThing();
&lt;/code&gt;

Then, even though it looks like there's no global variable, your code assumes that each and every instance of &lt;code&gt;MyMonostate&lt;/code&gt; shares state, and that a change to one changes the other, and that this state is accessible globally. This is a global variable, plain an simple.</description>
		<content:encoded><![CDATA[<p>I sort of think that we&#8217;ve come to the end of this argument, I don&#8217;t think I will convince you, and accept that if these ideas work for you then go for them.</p>
<p>However, the thing about Monostate needs to be cleared up:</p>
<p><em>The monostate uses private static variables. The scoping of these variables is the class itself. They are not accessible from outside this scope and hence are not global.</em></p>
<p>No matter how much you hide the global variable (in this case the state of the monostate class, which is kept in static variables, which makes it a collection of global variables) it will remain a global variable.</p>
<p>If you sprinkle your code with <code>new MyMonostate()</code> instead of <code>MySingleton.getInstance()</code> you will have tied your application to a concrete implementation and a global variable, regarless of the global variable appearing in disguise as multiple instances. Since all instances have the same state, which is global, they are nothing more than a facade to a collection of global variables.</p>
<p>The monostate may not look like a global variable, and you can pretend that it isn&#8217;t, but you can&#8217;t change the design later to allow for multiple instances because your code acts like it was all the same instance. This is exactly the same problem as using a singleton by accessing it with it&#8217;s <code>getInstance</code> all over the place.</p>
<p>If your code looks like this:</p>
<p><code><br />
var myMonostate = new MyMonostate();</p>
<p>myMonostate.setTheThing(3);</p>
<p>//&#8230; snip &#8230;</p>
<p>var myMonostate = new MyMonostate();</p>
<p>var theThing = myMonostate.getTheThing();<br />
</code></p>
<p>Then, even though it looks like there&#8217;s no global variable, your code assumes that each and every instance of <code>MyMonostate</code> shares state, and that a change to one changes the other, and that this state is accessible globally. This is a global variable, plain an simple.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: richard</title>
		<link>http://www.bigroom.co.uk/blog/improving-cairngorms-modellocator-part-6/#comment-27318</link>
		<pubDate>Mon, 26 May 2008 16:23:12 +0000</pubDate>
		<guid>http://www.bigroom.co.uk/blog/improving-cairngorms-modellocator-part-6/#comment-27318</guid>
					<description>Mate looks very interesting. After a brief look I like what I see and I will take a longer look at it soon. Thank you for bringing it to my attention..

For the counter-counter-counter comments...

&lt;i&gt;As you very well know the key feature of Monostate is that the state is kept in static variables. Static variables are by definition globals.&lt;/i&gt;

The monostate uses private static variables. The scoping of these variables is the class itself. They are not accessible from outside this scope and hence are not global.

&lt;i&gt;MyMonostate.myVar and (new MyMonostate()).myVar are equivalent statements and both access a global variable.&lt;/i&gt;

If myVar is private then the first access method doesn't work, so they are not equivalent.

&lt;i&gt;--Being a function, we can alter what it returns by altering the function body. This enables us to modify the object returned without altering the classes that use the object. (about part 5, the singleton factory)&lt;/i&gt;

&lt;i&gt;Except that that is the opposite of what you describe in part 5...&lt;/i&gt;

I describe in my comment above how to improve the function to allow this, by passing an interface rather than a class, and also apologise for not applying this in part 5.

&lt;i&gt;However, I think you’ve made a good effort of trying to avoid the major shortcoming of Cairngorm, but I also think that you need to look more deeply at what, exactly, the problem really is...&lt;/i&gt;

As I said above - "I can see a very strong argument for creating an alternative to Cairngorm that uses DI throughout but it was not my intent here to advocate scrapping Cairngorm, I’m just looking to improve it."

Cairngorm has the name of Adobe behind it and, like it or not, many developers will use it regardless of any shortcomings it has. That we can alter the way we manage the model within Cairngorm without having to alter or override any of the source code for Cairngorm itself provides one simple way to, perhaps, improve it by improving how people use it. This series of posts explores this idea.</description>
		<content:encoded><![CDATA[<p>Mate looks very interesting. After a brief look I like what I see and I will take a longer look at it soon. Thank you for bringing it to my attention..</p>
<p>For the counter-counter-counter comments&#8230;</p>
<p><i>As you very well know the key feature of Monostate is that the state is kept in static variables. Static variables are by definition globals.</i></p>
<p>The monostate uses private static variables. The scoping of these variables is the class itself. They are not accessible from outside this scope and hence are not global.</p>
<p><i>MyMonostate.myVar and (new MyMonostate()).myVar are equivalent statements and both access a global variable.</i></p>
<p>If myVar is private then the first access method doesn&#8217;t work, so they are not equivalent.</p>
<p><i>&#8211;Being a function, we can alter what it returns by altering the function body. This enables us to modify the object returned without altering the classes that use the object. (about part 5, the singleton factory)</i></p>
<p><i>Except that that is the opposite of what you describe in part 5&#8230;</i></p>
<p>I describe in my comment above how to improve the function to allow this, by passing an interface rather than a class, and also apologise for not applying this in part 5.</p>
<p><i>However, I think you’ve made a good effort of trying to avoid the major shortcoming of Cairngorm, but I also think that you need to look more deeply at what, exactly, the problem really is&#8230;</i></p>
<p>As I said above - &#8220;I can see a very strong argument for creating an alternative to Cairngorm that uses DI throughout but it was not my intent here to advocate scrapping Cairngorm, I’m just looking to improve it.&#8221;</p>
<p>Cairngorm has the name of Adobe behind it and, like it or not, many developers will use it regardless of any shortcomings it has. That we can alter the way we manage the model within Cairngorm without having to alter or override any of the source code for Cairngorm itself provides one simple way to, perhaps, improve it by improving how people use it. This series of posts explores this idea.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: richard</title>
		<link>http://www.bigroom.co.uk/blog/improving-cairngorms-modellocator-part-6/#comment-27317</link>
		<pubDate>Mon, 26 May 2008 16:02:51 +0000</pubDate>
		<guid>http://www.bigroom.co.uk/blog/improving-cairngorms-modellocator-part-6/#comment-27317</guid>
					<description>&lt;i&gt;--Removing globals is not the goal. Making code easier to develop and maintain is.&lt;/i&gt;

&lt;i&gt;You do realise that this is a contradiction?&lt;/i&gt;

No. If making code easier to develop and maintain is the goal, then removing globals can only be the goal if the two are synonymous. I don't agree with such absolutism. There may be other ways to make the code easier to develop and maintain, and removing globals doesn't always make the code easier to develop and maintain (although often it does). So, no I don't see why my statement is a contradiction.</description>
		<content:encoded><![CDATA[<p><i>&#8211;Removing globals is not the goal. Making code easier to develop and maintain is.</i></p>
<p><i>You do realise that this is a contradiction?</i></p>
<p>No. If making code easier to develop and maintain is the goal, then removing globals can only be the goal if the two are synonymous. I don&#8217;t agree with such absolutism. There may be other ways to make the code easier to develop and maintain, and removing globals doesn&#8217;t always make the code easier to develop and maintain (although often it does). So, no I don&#8217;t see why my statement is a contradiction.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Theo</title>
		<link>http://www.bigroom.co.uk/blog/improving-cairngorms-modellocator-part-6/#comment-27316</link>
		<pubDate>Mon, 26 May 2008 14:53:50 +0000</pubDate>
		<guid>http://www.bigroom.co.uk/blog/improving-cairngorms-modellocator-part-6/#comment-27316</guid>
					<description>&lt;em&gt;I don’t have much experience with DI frameworks so would value your opinion on this.&lt;/em&gt;

It's not a DI framework as such, but have a look at &lt;a href="http://mate.asfusion.com/" rel="nofollow"&gt;Mate&lt;/a&gt;, it's a Flex application framework that doesn't go down the global variables-route like the rest of them. 

Instead it does what the others say they do: make it easier to write reusable, decoupled code. It's new and untested, but given the state of Flex application frameworks I think it's more or less the only contender.

I don't think that DI needs a framework to be workable, for me it's more of an approach to architecture, the don't-call-us-we'll-call-you (a.k.a the Hollywood principle) makes it easier to design for low coupling and high reusability.



Now, to some counter-counter-comments:

&lt;em&gt;The only thing that is global [in Monostate] is the class constructor, but all public classes have class constructors that are globally accessible.&lt;/em&gt;

As you very well know the key feature of Monostate is that the state is kept in static variables. Static variables are by definition globals. It doesn't matter that you create instances of a monostate, because the instances are only access points to the global variables. &lt;code&gt;MyMonostate.myVar&lt;/code&gt; and &lt;code&gt;(new MyMonostate()).myVar&lt;/code&gt; are equivalent statements and both access a global variable. This is what I mean by Monostate being a facade to global variables, in the second example  it doesn't look like you access a global variable, but that is in effect what you do.


&lt;em&gt;Being a function, we can alter what it returns by altering the function body. This enables us to modify the object returned without altering the classes that use the object.&lt;/em&gt; (about part 5, the singleton factory)

Except that that is the opposite of what you describe in part 5. You describe a registry which keeps instances of classes that &lt;em&gt;you pass to it&lt;/em&gt;. You explicitly tell the registry that you want the instance of a specific type. By design there is no possibility to alter what the factory function returns.

The point of factory method pattern is to decouple you from the instantiation and concrete implementation of that which the factory creates, usually by specifying that it returns an interface type. In your version you specify the class (i.e. the concrete implementation) explicitly, which removes that benefit.

Your singleton factory is a place to store global variables. It moves the responsibility of enforcing the singleton-ness, but it is still just a variation of global variables.


To sum up my criticism of your suggestions: global variables are never a good idea because they make an application tightly coupled, leads to less reusable, less testable and less modularisable code. All your suggestions are just variations of global variables and have the same bad consequences. In my opinion no better than what Cairngorm offers by default, although that is pretty bad to start with.

However, I think you've made a good effort of trying to avoid the major shortcoming of Cairngorm, but I also think that you need to look more deeply at &lt;em&gt;what, exactly, the problem really is&lt;/em&gt;. It is not that Cairngorm uses the Singleton pattern, the problem is that it is designed to use global variables. To solve the problem the global variables must go and with them, most likely, the singletons, but hiding the singletons will not solve anything.</description>
		<content:encoded><![CDATA[<p><em>I don’t have much experience with DI frameworks so would value your opinion on this.</em></p>
<p>It&#8217;s not a DI framework as such, but have a look at <a href="http://mate.asfusion.com/" rel="nofollow">Mate</a>, it&#8217;s a Flex application framework that doesn&#8217;t go down the global variables-route like the rest of them. </p>
<p>Instead it does what the others say they do: make it easier to write reusable, decoupled code. It&#8217;s new and untested, but given the state of Flex application frameworks I think it&#8217;s more or less the only contender.</p>
<p>I don&#8217;t think that DI needs a framework to be workable, for me it&#8217;s more of an approach to architecture, the don&#8217;t-call-us-we&#8217;ll-call-you (a.k.a the Hollywood principle) makes it easier to design for low coupling and high reusability.</p>
<p>Now, to some counter-counter-comments:</p>
<p><em>The only thing that is global [in Monostate] is the class constructor, but all public classes have class constructors that are globally accessible.</em></p>
<p>As you very well know the key feature of Monostate is that the state is kept in static variables. Static variables are by definition globals. It doesn&#8217;t matter that you create instances of a monostate, because the instances are only access points to the global variables. <code>MyMonostate.myVar</code> and <code>(new MyMonostate()).myVar</code> are equivalent statements and both access a global variable. This is what I mean by Monostate being a facade to global variables, in the second example  it doesn&#8217;t look like you access a global variable, but that is in effect what you do.</p>
<p><em>Being a function, we can alter what it returns by altering the function body. This enables us to modify the object returned without altering the classes that use the object.</em> (about part 5, the singleton factory)</p>
<p>Except that that is the opposite of what you describe in part 5. You describe a registry which keeps instances of classes that <em>you pass to it</em>. You explicitly tell the registry that you want the instance of a specific type. By design there is no possibility to alter what the factory function returns.</p>
<p>The point of factory method pattern is to decouple you from the instantiation and concrete implementation of that which the factory creates, usually by specifying that it returns an interface type. In your version you specify the class (i.e. the concrete implementation) explicitly, which removes that benefit.</p>
<p>Your singleton factory is a place to store global variables. It moves the responsibility of enforcing the singleton-ness, but it is still just a variation of global variables.</p>
<p>To sum up my criticism of your suggestions: global variables are never a good idea because they make an application tightly coupled, leads to less reusable, less testable and less modularisable code. All your suggestions are just variations of global variables and have the same bad consequences. In my opinion no better than what Cairngorm offers by default, although that is pretty bad to start with.</p>
<p>However, I think you&#8217;ve made a good effort of trying to avoid the major shortcoming of Cairngorm, but I also think that you need to look more deeply at <em>what, exactly, the problem really is</em>. It is not that Cairngorm uses the Singleton pattern, the problem is that it is designed to use global variables. To solve the problem the global variables must go and with them, most likely, the singletons, but hiding the singletons will not solve anything.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Theo</title>
		<link>http://www.bigroom.co.uk/blog/improving-cairngorms-modellocator-part-6/#comment-27315</link>
		<pubDate>Mon, 26 May 2008 14:12:27 +0000</pubDate>
		<guid>http://www.bigroom.co.uk/blog/improving-cairngorms-modellocator-part-6/#comment-27315</guid>
					<description>&lt;em&gt;Removing globals is not the goal. Making code easier to develop and maintain is.&lt;/em&gt;

You do realise that this is a contradiction?</description>
		<content:encoded><![CDATA[<p><em>Removing globals is not the goal. Making code easier to develop and maintain is.</em></p>
<p>You do realise that this is a contradiction?
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: richard</title>
		<link>http://www.bigroom.co.uk/blog/improving-cairngorms-modellocator-part-6/#comment-27298</link>
		<pubDate>Fri, 23 May 2008 10:48:39 +0000</pubDate>
		<guid>http://www.bigroom.co.uk/blog/improving-cairngorms-modellocator-part-6/#comment-27298</guid>
					<description>&lt;i&gt;Theo said - As far as I have seen all your alternatives are just variations of hiding the global variable (a.k.a the singleton) so that it doesn’t look like a global variable. It doesn't actually solve the problem, it just makes it less obvious.&lt;/i&gt; 

Removing globals is not the goal. Making code easier to develop and maintain is.

&lt;i&gt;The solution in Part 2 is essentially Monostate, at least in practice. So is the solution in Part 3.&lt;/i&gt;

The solution in part 3 is just an improvement on part 2 so they can be considered as one. The interesting part of this solution is what happens behind the scenes, where the code is a hybrid of singleton and dependency injection through binding. Each instance has its own data, separate from the other instances. The required data is injected into the instance by the singleton, via the bindings. But from the outside it looks like a monostate.

&lt;i&gt;In Part 4 you describe an actual Monostate, which is nothing more than global variables behind a facade.&lt;/i&gt;

A monostate is a class whose instances share some common state between them. The only thing that is global is the class constructor, but all public classes have class constructors that are globally accessible. This is not a bad thing.

&lt;i&gt;In Part 5, finally, you describe a static factory, which, when used this way, is just another kind of global variable.&lt;/i&gt;

Being a function, we can alter what it returns by altering the function body. This enables us to modify the object returned without altering the classes that use the object. This is a distinct improvement on a global variable. It can be improved further – passing an interface to the function rather than a class, and having a concrete instance that implements the interface returned, would be one improvement. I grabbed the function from a previous post without thinking too hard if it could be improved for the purpose here. I apologise for that.

&lt;i&gt;The problem isn't that Cairngorm has singletons, the problem is that it uses them as global variables.&lt;/i&gt;

Global access is one of the two properties of the Singleton pattern. If it doesn't have global access, then it isn't a Singleton. And if one doesn't intend to use the global access one shouldn't use the Singleton pattern.

&lt;i&gt;I suggest looking at solutions that don't involve global variables at all, like Dependency Injection.&lt;/i&gt;

A simple DI implementation would involve passing the model data around from class to class, probably passing it into the constructor of each class that needs it. This works and I would advocate it on a small project. But this naïve implementation of DI can get unwieldy with larger projects. At that point one might turn to a DI framework of some form.

But why use both a DI framework and Cairngorm? If one uses DI, it would seem a good idea to replace both the Service Locator and the Model Locator with DI oriented implementations, and then there's not much of Cairngorm left. I can see a very strong argument for creating an alternative to Cairngorm that uses DI throughout but it was not my intent here to advocate scrapping Cairngorm, I'm just looking to improve it. Forcing Cairngorm to use DI feels like forcing a square peg into a round hole. I don't have much experience with DI frameworks so would value your opinion on this.</description>
		<content:encoded><![CDATA[<p><i>Theo said - As far as I have seen all your alternatives are just variations of hiding the global variable (a.k.a the singleton) so that it doesn’t look like a global variable. It doesn&#8217;t actually solve the problem, it just makes it less obvious.</i> </p>
<p>Removing globals is not the goal. Making code easier to develop and maintain is.</p>
<p><i>The solution in Part 2 is essentially Monostate, at least in practice. So is the solution in Part 3.</i></p>
<p>The solution in part 3 is just an improvement on part 2 so they can be considered as one. The interesting part of this solution is what happens behind the scenes, where the code is a hybrid of singleton and dependency injection through binding. Each instance has its own data, separate from the other instances. The required data is injected into the instance by the singleton, via the bindings. But from the outside it looks like a monostate.</p>
<p><i>In Part 4 you describe an actual Monostate, which is nothing more than global variables behind a facade.</i></p>
<p>A monostate is a class whose instances share some common state between them. The only thing that is global is the class constructor, but all public classes have class constructors that are globally accessible. This is not a bad thing.</p>
<p><i>In Part 5, finally, you describe a static factory, which, when used this way, is just another kind of global variable.</i></p>
<p>Being a function, we can alter what it returns by altering the function body. This enables us to modify the object returned without altering the classes that use the object. This is a distinct improvement on a global variable. It can be improved further – passing an interface to the function rather than a class, and having a concrete instance that implements the interface returned, would be one improvement. I grabbed the function from a previous post without thinking too hard if it could be improved for the purpose here. I apologise for that.</p>
<p><i>The problem isn&#8217;t that Cairngorm has singletons, the problem is that it uses them as global variables.</i></p>
<p>Global access is one of the two properties of the Singleton pattern. If it doesn&#8217;t have global access, then it isn&#8217;t a Singleton. And if one doesn&#8217;t intend to use the global access one shouldn&#8217;t use the Singleton pattern.</p>
<p><i>I suggest looking at solutions that don&#8217;t involve global variables at all, like Dependency Injection.</i></p>
<p>A simple DI implementation would involve passing the model data around from class to class, probably passing it into the constructor of each class that needs it. This works and I would advocate it on a small project. But this naïve implementation of DI can get unwieldy with larger projects. At that point one might turn to a DI framework of some form.</p>
<p>But why use both a DI framework and Cairngorm? If one uses DI, it would seem a good idea to replace both the Service Locator and the Model Locator with DI oriented implementations, and then there&#8217;s not much of Cairngorm left. I can see a very strong argument for creating an alternative to Cairngorm that uses DI throughout but it was not my intent here to advocate scrapping Cairngorm, I&#8217;m just looking to improve it. Forcing Cairngorm to use DI feels like forcing a square peg into a round hole. I don&#8217;t have much experience with DI frameworks so would value your opinion on this.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: richard</title>
		<link>http://www.bigroom.co.uk/blog/improving-cairngorms-modellocator-part-6/#comment-27296</link>
		<pubDate>Fri, 23 May 2008 08:48:15 +0000</pubDate>
		<guid>http://www.bigroom.co.uk/blog/improving-cairngorms-modellocator-part-6/#comment-27296</guid>
					<description>Eric - thanks. I wasn't aware of eclipse monkey. Will take a look.

Matt - The idea of the SingletonFactory is to separate the factory that manages the singleton from the class itself. The class becomes a normal class from which we can create as many instances as we like. The SingletonFactory is a way of creating and managing global access to a single instance of this class. This makes it possible to also create other, local, instances of the class if required or to reuse the class in another project where multiple instances are required.</description>
		<content:encoded><![CDATA[<p>Eric - thanks. I wasn&#8217;t aware of eclipse monkey. Will take a look.</p>
<p>Matt - The idea of the SingletonFactory is to separate the factory that manages the singleton from the class itself. The class becomes a normal class from which we can create as many instances as we like. The SingletonFactory is a way of creating and managing global access to a single instance of this class. This makes it possible to also create other, local, instances of the class if required or to reuse the class in another project where multiple instances are required.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Theo</title>
		<link>http://www.bigroom.co.uk/blog/improving-cairngorms-modellocator-part-6/#comment-27293</link>
		<pubDate>Thu, 22 May 2008 10:47:49 +0000</pubDate>
		<guid>http://www.bigroom.co.uk/blog/improving-cairngorms-modellocator-part-6/#comment-27293</guid>
					<description>As far as I have seen all your alternatives are just variations of hiding the global variable (a.k.a the singleton) so that it doesn't look like a global variable. It doesn't actually solve the problem, it just makes it less obvious.

The solution in Part 2 is essentially Monostate, at least in practice. So is the solution in Part 3. In Part 
Part 4 you describe an actual Monostate, which is nothing more than global variables behind a facade. In Part 5, finally, you describe a static factory, which, when used this way, is just another kind of global variable.

The problem isn't that Cairngorm has singletons, the problem is that it uses them as global variables. Your solutions are just variations on this problem. I suggest looking at solutions that don't involve global variables at all, like Dependency Injection.</description>
		<content:encoded><![CDATA[<p>As far as I have seen all your alternatives are just variations of hiding the global variable (a.k.a the singleton) so that it doesn&#8217;t look like a global variable. It doesn&#8217;t actually solve the problem, it just makes it less obvious.</p>
<p>The solution in Part 2 is essentially Monostate, at least in practice. So is the solution in Part 3. In Part<br />
Part 4 you describe an actual Monostate, which is nothing more than global variables behind a facade. In Part 5, finally, you describe a static factory, which, when used this way, is just another kind of global variable.</p>
<p>The problem isn&#8217;t that Cairngorm has singletons, the problem is that it uses them as global variables. Your solutions are just variations on this problem. I suggest looking at solutions that don&#8217;t involve global variables at all, like Dependency Injection.
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
