Jump to content


Photo

Question On Factory Pattern


  • Please log in to reply
2 replies to this topic

#1 Tutumon

Tutumon

    New Member

  • Members
  • Pip
  • 3 posts

Posted 11 May 2012 - 12:19 AM

public interface ICar
{
string Name { get; set; }
int Speed { get; set; }
void Drive();
}
public class Sedan : ICar
{
public string Name
{
get
{
return "Sedan Car";
}
set
{
Name = value;
}
}
public int Speed
{
get
{
return 100;
}
set
{
Speed = value;
}
}

public void Drive()
{
Console.WriteLine(Name + " is Running @ Speed " + Speed);
}
}
public class HatchBack : ICar
{
public string Name
{
get
{
return "Hatch Back Car";
}
set
{
Name = value;
}
}

public int Speed
{
get
{
return 120;
}
set
{
Speed = value;
}
}

public void Drive()
{
Console.WriteLine(Name + " is Running @ Speed " + Speed);
}
}
public class HatchFactory :CarFactory
{
public override ICar CreateCar()
{
return new HatchBack();
}
}
public class SedanFactory : CarFactory
{
public override ICar CreateCar()
{
return new Sedan();
}
}
class Program
{
static void Main(string[] args)
{
CarFactory factory = new SedanFactory();
ICar car = factory.CreateCar();
car.Drive();

factory = new HatchFactory();
car = factory.CreateCar();
car.Drive();

Console.Read();
}
}

Guys, can some one tell whether this is factory/abstract factory implementation

Thanks
Tutu

#2 Design Guru

Design Guru

    New Member

  • Members
  • Pip
  • 1 posts

Posted 31 May 2012 - 03:10 AM

This is an abstract fatory pattern implementation.

We have two factories here Sedanfactory and hatchbackfactory
The Cardactory is the abstract factory used here, thoug i could not find the defanition for that.

The abstract factory carfactory could point to either hatchfactory or sedanfactory at runtime.

We have abstract product Icar her ( normally we use an abstract class to define the abstract produt and not an interface)

sedan and hatchback are two concrete products


public interface ICar
{
string Name { get; set; }
int Speed { get; set; }
void Drive();
}
public class Sedan : ICar
{
public string Name
{
get
{
return "Sedan Car";
}
set
{
Name = value;
}
}
public int Speed
{
get
{
return 100;
}
set
{
Speed = value;
}
}

public void Drive()
{
Console.WriteLine(Name + " is Running @ Speed " + Speed);
}
}
public class HatchBack : ICar
{
public string Name
{
get
{
return "Hatch Back Car";
}
set
{
Name = value;
}
}

public int Speed
{
get
{
return 120;
}
set
{
Speed = value;
}
}

public void Drive()
{
Console.WriteLine(Name + " is Running @ Speed " + Speed);
}
}
public class HatchFactory :CarFactory
{
public override ICar CreateCar()
{
return new HatchBack();
}
}
public class SedanFactory : CarFactory
{
public override ICar CreateCar()
{
return new Sedan();
}
}
class Program
{
static void Main(string[] args)
{
CarFactory factory = new SedanFactory();
ICar car = factory.CreateCar();
car.Drive();

factory = new HatchFactory();
car = factory.CreateCar();
car.Drive();

Console.Read();
}
}

Guys, can some one tell whether this is factory/abstract factory implementation

Thanks
Tutu



#3 joeadk

joeadk

    New Member

  • Members
  • Pip
  • 1 posts

Posted 19 August 2012 - 10:46 PM

This is an abstract fatory pattern implementation.

We have two factories here Sedanfactory and hatchbackfactory
The Cardactory is the abstract factory used here, thoug i could not find the defanition for that.

The abstract factory carfactory could point to either hatchfactory or sedanfactory at runtime.

We have abstract product Icar her ( normally we use an abstract class to define the abstract produt and not an interface)

sedan and hatchback are two concrete products


This is just a factory method. Just because he is using an abstract object doesn't mean it's an abstract factory. In this example we just have a single object being abstracted out: the ICar, through an abstract method call. This is basically the same example as in the HFDP book, except he is making cars instead of pizzas.

Abstract Factory deals with families of related objects. For example, in the GoF book, you have a an abstract Widget factory that, in turn, has factories for windows, and scroll-bars, etc. HFDP discusses this in terms of ingredients. Each pizza has a different creator for crust, cheese, etc. and they are all grouped into the the Ingredient Factory. Abstract Factory is more of a factory of factories.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users