C#
  Home arrow C# arrow Page 3 - Creational Patterns in C#
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Creational Patterns in C#
By: Rajesh V S
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 56
    2003-05-15

    Table of Contents:
  • Creational Patterns in C#
  • The Factory Method Pattern
  • The Abstract Factory Pattern
  • The Builder Pattern
  • The Prototype Pattern
  • Summary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Creational Patterns in C# - The Abstract Factory Pattern


    (Page 3 of 6 )

    The Abstract Factory Pattern comes under the classification of Creational Patterns. The creational patterns deals with the best way to create objects. The Abstract Factory provides an interface to create and return one of several families of related objects.

    “Provide an interface for creating families of related or dependent objects without specifying their concrete classes” -- "Design Patterns” Gamma et al., Addison-Wesley, ISBN:0-201-63361-2”

    Non-software Example

    This pattern is found in the sheet metal stamping equipment used in the manufacture of Japanese automobiles. The stamping equipment is an Abstract Factory, which creates auto body parts. The same machinery is used to stamp right hand doors, left hand doors, right front fenders, left front fenders, hoods, etc. for different models of cars. Through the use of rollers to change the stamping dies, the concrete classes produced by the machinery can be changed within three minutes.

    [Michael Duell, "Non-software examples of software design patterns", Object Magazine, Jul 97, p54]

    The abstract factory is a factory object that returns one of several factories. It can be used to return one of several related classes of objects, each of which can return several different objects on request.

    The abstract factory pattern can be interpreted and implemented in many ways. The following is a simples interpretation and implementation of this pattern.

    In this case the interface Factory has two concrete implementations, ConcreteFactory1 and ConcreteFactory2. The getObject() inside these concrete classes returns Derived1 and Derived2 objects respectively. The client can decide which ConcreteFactory class has to be used during the run-times.

    The following is a more complicated interpretation and implementation of this pattern. Here those Factory class methods are used for returning objects of two different class hierarchies.

    C# Implementation

    // Creational Pattern: Abstract Factory Pattern
    //Author:
    rajeshvs@msn.com
    /*
    In the following snippet, Factory is an interface. The concrete implementation of this
    interface ConcreteFactory1 and ConcreteFactory2 implements the method getObject so
    that it returns Derived1 and Derived2 objects respectively. The Base is an interface and
    Derived1 and Derived2 are the concrete implementations of the base class. The client
    (MyClient class) always uses the Factory implementations to create an instance of the
    Base classes. Actually the derived classes of Factory interface decided which object
    (either Derived1 or Derived2) has to be created.
    */
    using System;
    interface Factory
    {
      Base GetObject();
    }
    //This class is responsible for creating objects of the class Derived1.
    class ConcreteFactory1 :Factory
    {
      public Base GetObject()
      {
    return new Derived1();  
      }
    }
    //This class is responsible for creating objects of the class Derived2.
    class ConcreteFactory2 : Factory
    {
      public Base GetObject()
      {
     return new Derived2();  
      }
    }
    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 he ConcreteFactory1.
    */
    class MyClient

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

    More C# Articles
    More By Rajesh V S


     

    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#







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT