<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.3" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>twit88.com &#187; Creational</title>
	<link>http://twit88.com/blog</link>
	<description>Good judgement comes from experience, and experience comes from bad judgement.</description>
	<pubDate>Wed, 19 Nov 2008 06:30:21 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.3</generator>
	<language>en</language>
			<item>
		<title>Design Pattern in .NET 101 - Abstract Factory Pattern (Creational Pattern)</title>
		<link>http://twit88.com/blog/2008/02/15/design-pattern-in-net-101-abstract-factory-pattern-creational-pattern/</link>
		<comments>http://twit88.com/blog/2008/02/15/design-pattern-in-net-101-abstract-factory-pattern-creational-pattern/#comments</comments>
		<pubDate>Fri, 15 Feb 2008 15:23:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[Creational]]></category>

		<category><![CDATA[design pattern]]></category>

		<guid isPermaLink="false">http://twit88.com/blog/2008/02/15/design-pattern-in-net-101-abstract-factory-pattern-creational-pattern/</guid>
		<description><![CDATA[Download Sample Code
Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.
E.g. I have a DocumentFactory class that either returns DefaultDocumentFactory or CustomDocumentFactory dependening on the environment variable.

using System;
using System.Collections.Generic;
using System.Text;

namespace AbstractFactory
{
    abstract class DocumentFactory
    {
     [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://twit88.com/blog/wp-content/uploads/2008/02/abstractfactory.zip' title='abstractfactory.zip'>Download Sample Code</a></p>
<p><strong>Abstract Factory</strong> pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.</p>
<p>E.g. I have a <strong>DocumentFactory</strong> class that either returns <strong>DefaultDocumentFactory</strong> or <strong>CustomDocumentFactory</strong> dependening on the environment variable.</p>
<pre>
using System;
using System.Collections.Generic;
using System.Text;

namespace AbstractFactory
{
    abstract class DocumentFactory
    {
        public static DocumentFactory GetFactory()
        {
            string documentType =
             System.Environment.GetEnvironmentVariable("DOC_TYPE");
            if (documentType == null || documentType == "")
            {
                return new DefaultDocumentFactory();
            }
            else
            {
                return new CustomDocumentFactory();
            }
        }

        public abstract Document CreateDocument();
    }
}
</pre>
<p><strong>DefaultDocumentFactory</strong></p>
<pre>
using System;
using System.Collections.Generic;
using System.Text;

namespace AbstractFactory
{
    class DefaultDocumentFactory: DocumentFactory
    {
        public override Document CreateDocument()
        {
            return new DefaultDocument();
        }
    }
}
</pre>
<p><strong>CustomDocumentFactory</strong></p>
<pre>
using System;
using System.Collections.Generic;
using System.Text;

namespace AbstractFactory
{
    class CustomDocumentFactory: DocumentFactory
    {
        public override Document CreateDocument()
        {
            return new CustomDocument();
        }
    }
}
</pre>
<p><strong>Document</strong>, <strong>DefaultDocument</strong> and <strong>CustomDocument</strong> are defined.</p>
<pre>
using System;
using System.Collections.Generic;
using System.Text;

namespace AbstractFactory
{
    abstract class Document
    {
      public string content;

        public String Content
        {
            get
            {
                return this.content;
            }
            set
            {
                this.content = value;
            }

        }

        public void Print()
        {
            Console.WriteLine(content);
        }

        public abstract void Write();

    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace AbstractFactory
{
    class DefaultDocument: Document
    {
        public override void Write()
        {
            this.Content = "Default Document";
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace AbstractFactory
{
    class CustomDocument:Document
    {
        public override void Write()
        {
            this.Content = "Custom Document";
        }
    }
}
</pre>
<p>To test it,</p>
<pre>
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace AbstractFactory
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            DocumentFactory  factory = DocumentFactory.GetFactory();
            Document document = factory.CreateDocument();
            document.Write();
            document.Print();
            Console.Read();
        }
    }
}</pre>
<p>The output</p>
<pre>
Default Document
</pre>
]]></content:encoded>
			<wfw:commentRss>http://twit88.com/blog/2008/02/15/design-pattern-in-net-101-abstract-factory-pattern-creational-pattern/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Design Pattern in .NET 101 - Factory Method Pattern (Creational Pattern)</title>
		<link>http://twit88.com/blog/2008/01/12/design-pattern-in-net-101-factory-method-pattern-creational-pattern/</link>
		<comments>http://twit88.com/blog/2008/01/12/design-pattern-in-net-101-factory-method-pattern-creational-pattern/#comments</comments>
		<pubDate>Sat, 12 Jan 2008 13:09:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[Creational]]></category>

		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://twit88.com/blog/2008/01/12/design-pattern-in-net-101-factory-method-pattern-creational-pattern/</guid>
		<description><![CDATA[Download Sample Code
Factory Method Pattern defines an interface for creating an object, but let subclasses decide which class to instantiate. It lets a class defer instantiation to subclasses. 
E.g., I defined a abstract News class.

using System;
using System.Collections.Generic;
using System.Text;

namespace FactoryPattern
{
    abstract class News
    {
    }
}

Different kinds of [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://twit88.com/blog/wp-content/uploads/2008/01/factorypattern.zip' title='factorypattern.zip'>Download Sample Code</a></p>
<p><strong>Factory Method Pattern</strong> defines an interface for creating an object, but let subclasses decide which class to instantiate. It lets a class defer instantiation to subclasses. </p>
<p>E.g., I defined a abstract <strong>News</strong> class.</p>
<pre>
using System;
using System.Collections.Generic;
using System.Text;

namespace FactoryPattern
{
    abstract class News
    {
    }
}
</pre>
<p>Different kinds of news are defined..</p>
<pre>
using System;
using System.Collections.Generic;
using System.Text;

namespace FactoryPattern
{
    class AdNews:News
    {
    }
}
</pre>
<pre>
using System;
using System.Collections.Generic;
using System.Text;

namespace FactoryPattern
{
    class FashionNews:News
    {
    }
}
</pre>
<pre>
using System;
using System.Collections.Generic;
using System.Text;

namespace FactoryPattern
{
    class FinanceNews:News
    {
    }
}
</pre>
<pre>
using System;
using System.Collections.Generic;
using System.Text;

namespace FactoryPattern
{
    class SportsNews:News
    {
    }
}
</pre>
<pre>
using System;
using System.Collections.Generic;
using System.Text;

namespace FactoryPattern
{
    class WorldNews:News
    {
    }
}
</pre>
<p>I defined the abstract factory class, <strong>Publication</strong></p>
<pre>
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace FactoryPattern
{
    abstract class Publication
    {
        private ArrayList publishedNews = new ArrayList();

        // Constructor calls abstract Factory method
        public Publication()
        {
            this.PublishNews();
        }

        public ArrayList PublishedNews
        {
            get
            {
                return publishedNews;
            }
        }

        // Factory Method
        public abstract void PublishNews();
    }
}
</pre>
<p>In the <strong>Publication</strong> class, the factory method is called, but it is not implemented yet.</p>
<p><strong>Newspaper</strong> and <strong>FashionMagazine</strong> extends <strong>Publication</strong> class.</p>
<pre>
using System;
using System.Collections.Generic;
using System.Text;

namespace FactoryPattern
{
    class NewsPaper:Publication
    {
        public override void PublishNews()
        {
            this.PublishedNews.Add(new AdNews());
            this.PublishedNews.Add(new FashionNews());
            this.PublishedNews.Add(new WorldNews());
            this.PublishedNews.Add(new SportsNews());
            this.PublishedNews.Add(new FinanceNews());
        }
    }
}
</pre>
<pre>
using System;
using System.Collections.Generic;
using System.Text;

namespace FactoryPattern
{
    class FashionMagazine:Publication
    {
        public override void PublishNews()
        {
            this.PublishedNews.Add(new AdNews());
            this.PublishedNews.Add(new FashionNews());
        }
    }
}
</pre>
<p>To test it</p>
<pre>
using System;
using System.Collections.Generic;
using System.Text;

namespace FactoryPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Publication newsPaper = new NewsPaper();
            Console.WriteLine(newsPaper.GetType().Name);
            foreach (News news in newsPaper.PublishedNews)
            {
                Console.WriteLine("-" + news.GetType().Name);
            }

            Console.WriteLine();

            Publication fashionMagazine = new FashionMagazine();
            Console.WriteLine(fashionMagazine.GetType().Name);
            foreach (News news in fashionMagazine.PublishedNews)
            {
                Console.WriteLine("-" + news.GetType().Name);
            }

            Console.Read();
        }
    }
}
</pre>
<p>The output</p>
<pre>
NewsPaper
-AdNews
-FashionNews
-WorldNews
-SportsNews
-FinanceNews

FashionMagazine
-AdNews
-FashionNews
</pre>
]]></content:encoded>
			<wfw:commentRss>http://twit88.com/blog/2008/01/12/design-pattern-in-net-101-factory-method-pattern-creational-pattern/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
