Click here to Skip to main content
15,880,543 members
Articles / Static
Article

Factory Pattern

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL1 min read 5.4K   1  
Factory method is "Define an interface for creating an object, but let the subclasses decide which class to instantiate." In simple words we

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Factory method is "Define an interface for creating an object, but let the subclasses decide which class to instantiate." 

In simple words we develop an abstraction that isolates the logic for determining which type of class to create.

All Concept clear with below example.wo

You go to coffee shop and Want to order coffee. There are two type of coffee

- Hot

- Cold

Suppose in menu at number one is Hot and on two is cold

Which number you will tell, that order will processed.

To solve in factory way. Will make one abstract class and two sub classes,

public abstract class Coffee
    {
        public abstract string Title { get; }
    }

    public class Hot : Coffee
    {
        public override string Title
        {
            get { return "Hot Coffee with Suger"; }
        }
    }

    public class Cold : Coffee
    {
        public override string Title
        {
            get { return "Cold Coffee with Ice"; }
        }
    }

// In case coffee is not available

    public class NoCoffee : Coffee
    {
        public override string Title
        {
            get { return "Today no coffee.. Would you like some drink :) "; }
        }
    }
   

Now will make an inermediate which will receive input from user and will create object of relevent class.

 public static class Factory
    {
        public static Coffee GetCoffee(int i)
        {
            switch (i)
            {
                case 0:
                    return new Hot();

                case 1:
                    return new Cold();

              
                default:
                    return new NoCoffee();

            }
        }
    }

Ok All work is complete except Order

Only need to give input

static void Main(string[] args)
        {
          Console.WriteLine( Factory.GetCoffee(1).Title);
        }

It will show Hot Coffee

For source code, visit on below url:


http://www.xpode.com/ShowArticle.aspx?ArticleId=610

This article was originally posted at http://wiki.asp.net/page.aspx/1780/factory-pattern

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

755 members

Comments and Discussions

 
-- There are no messages in this forum --