Ans:
Initially an object is created with the "new" operator. That basic mechanism of object creation could result in design problems or added complexity to the design. On each Object creation we must use the new keyword. The Factory helps you to reduce this practice and use the common interface to create an object.
The Factory Pattern is a Creational Pattern that simplifies object creation. You need not worry about the object creation; you just need to supply an appropriate parameter and factory to provide you a product as needed.
In Factory pattern, we create the 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
Example:
class Program
{
static void Main(string[] args)
{
VehicalFactroy objnew = new createrVehical();
IFactory scooter = objnew.GetVehical("S");
scooter.VehicalMiles(10);
}
}
public interface IFactory
{
int VehicalMiles(int miles);
}
public class Scooter:IFactory
{
public Scooter()
{
Console.WriteLine("Scooter");
}
public int VehicalMiles(int miles)
{
Console.WriteLine("Scooter:" + miles);
return miles;
}
}
public class Bike:IFactory
{
public Bike()
{
Console.WriteLine("BIKE");
}
public int VehicalMiles(int miles)
{
Console.WriteLine("Bike:" + miles);
return miles;
}
}
public class Audi:IFactory
{
public Audi()
{
Console.WriteLine("Audi");
}
public int VehicalMiles(int miles)
{
Console.WriteLine("Bike:" + miles);
return miles;
}
}
public abstract class VehicalFactroy
{
public abstract IFactory GetVehical(string str);
}
public class createrVehical:VehicalFactroy
{
public override IFactory GetVehical(string str1)
{
switch (str1)
{
case "S": return new Scooter();
case "B": return new Bike();
case "A": return new Audi();
default: return new Scooter();
}
}
}
Initially an object is created with the "new" operator. That basic mechanism of object creation could result in design problems or added complexity to the design. On each Object creation we must use the new keyword. The Factory helps you to reduce this practice and use the common interface to create an object.
The Factory Pattern is a Creational Pattern that simplifies object creation. You need not worry about the object creation; you just need to supply an appropriate parameter and factory to provide you a product as needed.
In Factory pattern, we create the 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
Example:
class Program
{
static void Main(string[] args)
{
VehicalFactroy objnew = new createrVehical();
IFactory scooter = objnew.GetVehical("S");
scooter.VehicalMiles(10);
}
}
public interface IFactory
{
int VehicalMiles(int miles);
}
public class Scooter:IFactory
{
public Scooter()
{
Console.WriteLine("Scooter");
}
public int VehicalMiles(int miles)
{
Console.WriteLine("Scooter:" + miles);
return miles;
}
}
public class Bike:IFactory
{
public Bike()
{
Console.WriteLine("BIKE");
}
public int VehicalMiles(int miles)
{
Console.WriteLine("Bike:" + miles);
return miles;
}
}
public class Audi:IFactory
{
public Audi()
{
Console.WriteLine("Audi");
}
public int VehicalMiles(int miles)
{
Console.WriteLine("Bike:" + miles);
return miles;
}
}
public abstract class VehicalFactroy
{
public abstract IFactory GetVehical(string str);
}
public class createrVehical:VehicalFactroy
{
public override IFactory GetVehical(string str1)
{
switch (str1)
{
case "S": return new Scooter();
case "B": return new Bike();
case "A": return new Audi();
default: return new Scooter();
}
}
}