There are different uses of Factory Method design pattern. but not make much sense to me. I believe this pattern is to create an instance from a class you don't exactly know, otherwise it is not that necessary.
The following is my way:
static void Main(string[] args)
{
Creater creater = new Creater();
Product product = (Product)creater.GetProduct(typeof(Bike));
product.name = "red flag";
product.model = "man";
if(product is Car)
{
Bike bike = (Bike)product;
bike.style = "light";
Console.WriteLine(string.Format("{0}-{1}-{2}", bike.name, bike.model, bike.style));
Console.ReadLine();
}
}
abstract class Product
{
public string model;
public string name;
}
class Bike : Product
{
public string style;
}
class Car : Product
{
public string produce;
}
interface ICreater
{
object GetProduct(Type type);
}
class Creater
{
public object GetProduct(Type type)
{
return Activator.CreateInstance(type);
}
public object GetProduct2(Product product)
{
return Activator.CreateInstance(typeof(product));
}
}
For further research:
http://www.ondotnet.com/pub/a/dotnet/2003/08/11/factorypattern.html
No comments:
Post a Comment