Factory Method Pattern using Generics-C#

Posted by nanda on Stack Overflow See other posts from Stack Overflow or by nanda
Published on 2010-03-23T12:52:50Z Indexed on 2010/03/23 13:03 UTC
Read the original article Hit count: 334

Filed under:
|
|

Just I am learning Generics.When i have an Abstract Method pattern like :

//Abstract Product
interface IPage
{
    string pageType();
}

//Concerete Product 1
class ResumePage : IPage
{
    public string pageType()
    {
        return "Resume Page";
    }
}

//Concrete Product 2
class SummaryPage : IPage
{
  public string pageType()
  {
    return "SummaryPage";
   }
}

//Fcatory Creator
class FactoryCreator
{
   public IPage CreateOnRequirement(int i)
    {
      if (i == 1) return new ResumePage();
      else { return new SummaryPage(); }
    }
}


//Client/Consumer

void Main()
{

  FactoryCreator c = new FactoryCreator();
  IPage p;
  p = c.CreateOnRequirement(1);
  Console.WriteLine("Page Type is {0}", p.pageType());
  p = c.CreateOnRequirement(2);
  Console.WriteLine("Page Type is {0}", p.pageType());
  Console.ReadLine();
}

how to convert the code using generics?

© Stack Overflow or respective owner

Related posts about c#

Related posts about design-patterns