<?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: Using the Factory Pattern</title>
	<atom:link href="http://www.jaltiere.com/index.php/2007/12/04/using-the-factory-pattern/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jaltiere.com/index.php/2007/12/04/using-the-factory-pattern/</link>
	<description>.NET Software Development</description>
	<lastBuildDate>Mon, 14 Nov 2011 14:58:38 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Jack Altiere</title>
		<link>http://www.jaltiere.com/index.php/2007/12/04/using-the-factory-pattern/comment-page-1/#comment-58308</link>
		<dc:creator>Jack Altiere</dc:creator>
		<pubDate>Thu, 22 Jul 2010 12:18:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.jaltiere.com/?p=32#comment-58308</guid>
		<description>@Finbar,

Let me know if you come up with something, that sounds like it could be a nice solution.</description>
		<content:encoded><![CDATA[<p>@Finbar,</p>
<p>Let me know if you come up with something, that sounds like it could be a nice solution.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Finbar Leahy</title>
		<link>http://www.jaltiere.com/index.php/2007/12/04/using-the-factory-pattern/comment-page-1/#comment-58301</link>
		<dc:creator>Finbar Leahy</dc:creator>
		<pubDate>Sat, 17 Jul 2010 21:08:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.jaltiere.com/?p=32#comment-58301</guid>
		<description>Thanks for that Jack.

I was thinking that perhaps it would be possible(it would be nice to avoid casting, if possible of course) to use an interface to accommodate functionality that is not specific to the base class. The interface, lets say IFilterSpecifics could be exposed by the base and all derived classes and thus the cast could be somehow avoided.</description>
		<content:encoded><![CDATA[<p>Thanks for that Jack.</p>
<p>I was thinking that perhaps it would be possible(it would be nice to avoid casting, if possible of course) to use an interface to accommodate functionality that is not specific to the base class. The interface, lets say IFilterSpecifics could be exposed by the base and all derived classes and thus the cast could be somehow avoided.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jack Altiere</title>
		<link>http://www.jaltiere.com/index.php/2007/12/04/using-the-factory-pattern/comment-page-1/#comment-58276</link>
		<dc:creator>Jack Altiere</dc:creator>
		<pubDate>Fri, 26 Mar 2010 14:48:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.jaltiere.com/?p=32#comment-58276</guid>
		<description>Finbar,

I dug out my old example and added a divide filter to it.  A few ideas for you...

1. I added a method to make sure that the division was valid right in the class and that works as is.  (in this case an exception is thrown for invalid division)

[Serializable]
public class DivideFilter : BaseFilter
{
    public override int NumberOperation(int number1, int number2)
    {
        if (isValidDivision(number1, number2))
        {
            return number1/number2;   
        }
        
        throw new Exception(&quot;Error: Division by Zero error!&quot;);
    }

    public bool isValidDivision(int number1, int number2)
    {
        return (number2 != 0);
    }
    
    public void Foo()
    {
        Console.WriteLine(&quot;This method is specific to the divide filter.&quot;);
    }
}

2. If you really need to have access to something in your base class it gets a little trickier.  I created a Foo method off my DivideFilter class above that is unique to that class.  I then exposed the filter from inside the FilterFactory class, then in my console application I just do a soft cast to trap the case where I have a DivideFilter, then I can call the method.  By doing a soft cast, no exception is thrown for other types of filters.

// Create a sample divide filter.
using (var fStream = new FileStream(&quot;divide.xml&quot;, FileMode.Create))
{
    var dFilter = new DivideFilter();
    var serializer = new XmlSerializer(typeof(BaseFilter));
    serializer.Serialize(fStream, dFilter);
}

factory.ReadFile(&quot;divide.xml&quot;);
Console.WriteLine(&quot;Answer for 5 and 2 is {0} [{1}]&quot;, factory.NumberOperation(5, 2), factory.Type);

filter = factory.Filter as DivideFilter;
if (filter != null)
{
    // This method is unique to the divide filter.
    filter.Foo();
}

Hope that helps!

Jack</description>
		<content:encoded><![CDATA[<p>Finbar,</p>
<p>I dug out my old example and added a divide filter to it.  A few ideas for you&#8230;</p>
<p>1. I added a method to make sure that the division was valid right in the class and that works as is.  (in this case an exception is thrown for invalid division)</p>
<p>[Serializable]<br />
public class DivideFilter : BaseFilter<br />
{<br />
    public override int NumberOperation(int number1, int number2)<br />
    {<br />
        if (isValidDivision(number1, number2))<br />
        {<br />
            return number1/number2;<br />
        }</p>
<p>        throw new Exception(&#8220;Error: Division by Zero error!&#8221;);<br />
    }</p>
<p>    public bool isValidDivision(int number1, int number2)<br />
    {<br />
        return (number2 != 0);<br />
    }</p>
<p>    public void Foo()<br />
    {<br />
        Console.WriteLine(&#8220;This method is specific to the divide filter.&#8221;);<br />
    }<br />
}</p>
<p>2. If you really need to have access to something in your base class it gets a little trickier.  I created a Foo method off my DivideFilter class above that is unique to that class.  I then exposed the filter from inside the FilterFactory class, then in my console application I just do a soft cast to trap the case where I have a DivideFilter, then I can call the method.  By doing a soft cast, no exception is thrown for other types of filters.</p>
<p>// Create a sample divide filter.<br />
using (var fStream = new FileStream(&#8220;divide.xml&#8221;, FileMode.Create))<br />
{<br />
    var dFilter = new DivideFilter();<br />
    var serializer = new XmlSerializer(typeof(BaseFilter));<br />
    serializer.Serialize(fStream, dFilter);<br />
}</p>
<p>factory.ReadFile(&#8220;divide.xml&#8221;);<br />
Console.WriteLine(&#8220;Answer for 5 and 2 is {0} [{1}]&#8220;, factory.NumberOperation(5, 2), factory.Type);</p>
<p>filter = factory.Filter as DivideFilter;<br />
if (filter != null)<br />
{<br />
    // This method is unique to the divide filter.<br />
    filter.Foo();<br />
}</p>
<p>Hope that helps!</p>
<p>Jack</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Finbar Leahy</title>
		<link>http://www.jaltiere.com/index.php/2007/12/04/using-the-factory-pattern/comment-page-1/#comment-58273</link>
		<dc:creator>Finbar Leahy</dc:creator>
		<pubDate>Wed, 24 Mar 2010 10:25:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.jaltiere.com/?p=32#comment-58273</guid>
		<description>Thanks for publishing this blog. Your writing technique is very clear. I have a query for you as I see an issue with your deserialisation methodology. Concrete classes in nearly all cases will have specific implementations e.g. a &quot;DivideFilter&quot; might have GetNumerator() and GetDenomitor() methods. These methods will be specific/to that filter alone and *not* relevant to the other concrete filters. This brings me to my question: how do we adjust the above so that we can deserialise data that is specific to a concrete class?</description>
		<content:encoded><![CDATA[<p>Thanks for publishing this blog. Your writing technique is very clear. I have a query for you as I see an issue with your deserialisation methodology. Concrete classes in nearly all cases will have specific implementations e.g. a &#8220;DivideFilter&#8221; might have GetNumerator() and GetDenomitor() methods. These methods will be specific/to that filter alone and *not* relevant to the other concrete filters. This brings me to my question: how do we adjust the above so that we can deserialise data that is specific to a concrete class?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt Law</title>
		<link>http://www.jaltiere.com/index.php/2007/12/04/using-the-factory-pattern/comment-page-1/#comment-23645</link>
		<dc:creator>Matt Law</dc:creator>
		<pubDate>Sun, 23 Mar 2008 13:38:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.jaltiere.com/?p=32#comment-23645</guid>
		<description>Hi,

Many thanks for this, you&#039;ve saved me hours, if not days of head scratching. I&#039;ve been avidly learning about GoF Design Patterns of late, and since much of my work involves web services and serialization.

Your well explained example will allow me to apply the factory pattern into my C#/AMF3 developments.

Many thanks.</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>Many thanks for this, you&#8217;ve saved me hours, if not days of head scratching. I&#8217;ve been avidly learning about GoF Design Patterns of late, and since much of my work involves web services and serialization.</p>
<p>Your well explained example will allow me to apply the factory pattern into my C#/AMF3 developments.</p>
<p>Many thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sam</title>
		<link>http://www.jaltiere.com/index.php/2007/12/04/using-the-factory-pattern/comment-page-1/#comment-16608</link>
		<dc:creator>Sam</dc:creator>
		<pubDate>Wed, 05 Dec 2007 07:33:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.jaltiere.com/?p=32#comment-16608</guid>
		<description>This is really interesting to me. It took me a bit to (generally) understand the code, but it does seem like an elegant solution.</description>
		<content:encoded><![CDATA[<p>This is really interesting to me. It took me a bit to (generally) understand the code, but it does seem like an elegant solution.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

