O'Reilly Forums: Question On Factory Pattern - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Question On Factory Pattern Need to know whether this is a valid Factory

#1 User is offline   Tutumon 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 23-February 11

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
0

#2 User is offline   Design Guru 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 31-May 12

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


View PostTutumon, on 11 May 2012 - 12:19 AM, said:

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

0

#3 User is offline   joeadk 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 19-August 12

Posted 19 August 2012 - 10:46 PM

View PostDesign Guru, on 31 May 2012 - 03:10 AM, said:

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users