In Factory Method pattern, we create object without exposing the creation logic. In this pattern, an interface is used for creating an object, but let subclass decide which class to instantiate. The creation of object is done when it is required. The Factory method allows a class later instantiation to subclasses.
C# - Implementation Code
interface Product
{
}
class ConcreteProductA : Product
{
}
class ConcreteProductB : Product
{
}
abstract class Creator
{
public abstract Product FactoryMethod(string type);
}
class ConcreteCreator : Creator
{
public override Product FactoryMethod(string type)
{
switch (type)
{
case "A": return new ConcreteProductA();
case "B": return new ConcreteProductB();
default: throw new ArgumentException("Invalid type", "type");
}
}
}
***************************************************************************************************************
Another C# - Implementation for FactoryMethod design pattern:
interface IIceCream
{
string Functionality();
}
class ChocolateIceCream : IIceCream
{
public string Functionality()
{
return "Chocolate Ice cream";
}
}
class VanillaIceCream : IIceCream
{
public string Functionality()
{
return "Vanilla Ice cream";
}
}
class StrawberryIceCream : IIceCream { public string Functionality()
{
return "Strawberry Ice cream";
}
}
This is the basic structure
of our program. Now we need to implement the "Factory Method", which
will create the instance of our desired ice-cream at run-time. So we
will create this method now.
This is how I’m going to do this.
static class Factory
{
/// <summary> /// This is the Factory method
/// </summary>
public static IIceCream Get(int id)
{
switch (id)
{
case 0:
return new ChocolateIceCream();
case 1:
return new VanillaIceCream();
case 2:
return new StrawberryIceCream(); default:
return null;
}
}
}
C# - Implementation Code
interface Product
{
}
class ConcreteProductA : Product
{
}
class ConcreteProductB : Product
{
}
abstract class Creator
{
public abstract Product FactoryMethod(string type);
}
class ConcreteCreator : Creator
{
public override Product FactoryMethod(string type)
{
switch (type)
{
case "A": return new ConcreteProductA();
case "B": return new ConcreteProductB();
default: throw new ArgumentException("Invalid type", "type");
}
}
}
***************************************************************************************************************
Another C# - Implementation for FactoryMethod design pattern:
interface IIceCream
{
string Functionality();
}
class ChocolateIceCream : IIceCream
{
public string Functionality()
{
return "Chocolate Ice cream";
}
}
class VanillaIceCream : IIceCream
{
public string Functionality()
{
return "Vanilla Ice cream";
}
}
class StrawberryIceCream : IIceCream { public string Functionality()
{
return "Strawberry Ice cream";
}
}
This is the basic structure
of our program. Now we need to implement the "Factory Method", which
will create the instance of our desired ice-cream at run-time. So we
will create this method now.
This is how I’m going to do this.
static class Factory
{
/// <summary> /// This is the Factory method
/// </summary>
public static IIceCream Get(int id)
{
switch (id)
{
case 0:
return new ChocolateIceCream();
case 1:
return new VanillaIceCream();
case 2:
return new StrawberryIceCream(); default:
return null;
}
}
}
No comments:
Post a Comment