Home arrow C# arrow Page 2 - Creational Patterns in C#
C#

Creational Patterns in C#


When it comes to asking questions about creating patterns with C#, Rajesh has all the answers. Read about some C# patterns in this article.

Author Info:
By: Rajesh V S
Rating: 4 stars4 stars4 stars4 stars4 stars / 71
May 15, 2003
TABLE OF CONTENTS:
  1. · Creational Patterns in C#
  2. · The Factory Method Pattern
  3. · The Abstract Factory Pattern
  4. · The Builder Pattern
  5. · The Prototype Pattern
  6. · Summary

print this article
SEARCH DEVARTICLES

Creational Patterns in C# - The Factory Method Pattern
(Page 2 of 6 )

The Factory Method Pattern comes under the classification of Creational Patterns. The creational patterns deals with the best way to create objects. The Factory Method provides a simple decision making class that can return the object of one of several subclasses of an abstract base class depending on the information that are provided.

“Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.” -- "Design Patterns” Gamma et al., Addison-Wesley, ISBN:0-201-63361-2”

Non-software Example

Injection molding presses demonstrate this pattern. Manufacturers of plastic toys process plastic molding powder, and inject the plastic into molds of the desired shapes. The class of toy (car, action figure, etc.) is determined by the mold. [Michael Duell, "Non-software examples of software design patterns", Object Magazine, Jul 97, p54]

A factory pattern is one that returns an instance of one of several possible classes depending on the data provided to it. Usually all of the classes it returns should have a common base class and common methods, but implementations of the methods may be different.

The following is an UML representation of the Factory Method Pattern. In this case we are not directly creating an instance of the class Derived1 and Derived2. Instead we are using the getObject() method of the Factory class to return an appropriate instance depending on the value passed to the getObject() method. This method is commonly knows as the Factory method and Factory method can be either static or non-static in nature.

C# Implementation

//Creational Pattern: The Factory Method
//Author:
rajeshvs@msn.com
/* In Factory method pattern, A Factory class contains a factory method is used for creating the object. This factory method can be either static or non-static. */
using System;
class Factory
{
 public Base GetObject(int type)
 {
  Base base1 = null;
  switch(type)
  {
   case 1:
     base1 = new Derived1();
     break;
   case 2:
     base1 = new Derived2();
     break;
  }
  return base1;
 }
}
interface Base
{
  void DoIt();
}
class Derived1 : Base
{
   public void DoIt()
   {
  Console.WriteLine("Derived 1 method");
   }
}
class Derived2 : Base
{
   public void DoIt()
   {
  Console.WriteLine("Derived 2 method");
   }
}
//Client class
//Client class needn’t know about instance creation. The creation of Product is //deferred to the Factory class
class MyClient

   public static void Main()
   {
      Factory factory = new Factory();//Decides which object must create.
      Base obj = factory.GetObject(2);
      obj.DoIt();
   }
}

This is what the fundamental principle of Factory pattern. We create an abstraction, which decides which of several possible classes to return, and returns one. After that we can call the methods of that class instance without ever knowing which derived class is using actually. The object creation happens in a single place that is inside the Factory class.

Remember that the Factory class can contain more than one Factory methods. Even these Factory methods can be either static or non-static.


blog comments powered by Disqus
C# ARTICLES

- Introduction to Objects and Classes in C#, P...
- Visual C#.NET, Part 1: Introduction to Progr...
- C# - An Introduction
- Hotmail Exposed: Access Hotmail using C#
- Razor Sharp C#
- Introduction to Objects and Classes in C#
- Making Your Code CLS Compliant
- Programming with MySQL and .NET Technologies
- Socket Programming in C# - Part II
- Socket Programming in C# - Part I
- Creational Patterns in C#
- Type Conversions
- Creating Custom Delegates and Events in C#
- Inheritance and Polymorphism
- Understanding Properties in C#

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials