Abstract Factory Pattern
The Abstract Factory provides an interface for creating families of related or dependent objects without specifying their concrete classes. Abstract Factory provides an interface for creating families of related objects without specifying their concrete classes. It… encapsulates object creation. relies on object composition—object creation is implemented in methods exposed in the factory interface. promotes loose coupling by reducing the dependency of the application on concrete classes.
The Abstract Factory provides an interface for creating families of related or dependent objects without specifying their concrete classes. Abstract Factory provides an interface for creating families of related objects without specifying their concrete classes. It… encapsulates object creation. relies on object composition—object creation is implemented in methods exposed in the factory interface. promotes loose coupling by reducing the dependency of the application on concrete classes.
class Program
{
static void Main(string[] args)
{
IAnimal IAnimal = null;
AnimalFactory objAnimalFactory = null;
objAnimalFactory = AnimalFactory.GetAnimalFactory("sea");
Console.Write("\n\n Animal factory type is " + objAnimalFactory.GetType());
Console.ReadKey();
IAnimal = objAnimalFactory.GetAnimal("Shark");
Console.Write("\n Animal type is " + IAnimal.GetType());
Console.Write("\n Animal Speak " + IAnimal.Speak());
Console.ReadKey();
IAnimal = objAnimalFactory.GetAnimal("Octopus");
Console.Write("\n Animal type is " + IAnimal.GetType());
Console.Write("\n Animal Speak " + IAnimal.Speak());
Console.ReadKey();
objAnimalFactory = AnimalFactory.GetAnimalFactory("land");
Console.Write("\n\n Animal factory type is " + objAnimalFactory.GetType());
Console.ReadKey();
IAnimal = objAnimalFactory.GetAnimal("Cat");
Console.Write("\n Animal type is " + IAnimal.GetType());
Console.Write("\n Animal Speak " + IAnimal.Speak());
Console.ReadKey();
IAnimal = objAnimalFactory.GetAnimal("Dog");
Console.Write("\n Animal type is " + IAnimal.GetType());
Console.Write("\n Animal Speak " + IAnimal.Speak());
Console.ReadKey();
IAnimal = objAnimalFactory.GetAnimal("Lion");
Console.Write("\n Animal type is " + IAnimal.GetType());
Console.Write("\n Animal Speak " + IAnimal.Speak());
Console.ReadKey();
}
}
//****************Abstract Factory Pattern**********************//
public interface IAnimal
{
string Speak();
}
public class Cat : IAnimal
{
public string Speak()
{
return "MIAOO";
}
}
public class Dog : IAnimal
{
public string Speak()
{
return "Bark";
}
}
public class Lion : IAnimal
{
public string Speak()
{
return "Roar";
}
}
public class Shark : IAnimal
{
public string Speak()
{
return "Can't Speak";
}
}
public class Octopus : IAnimal
{
public string Speak()
{
return "Can't Speak";
}
}
public abstract class AnimalFactory
{
public abstract IAnimal GetAnimal(string animalType);
public static AnimalFactory GetAnimalFactory(string factoryType)
{
AnimalFactory animalfactory = null;
if ("sea".Equals(factoryType))
{
animalfactory = new SeaAnimalFactory();
}
else
{
animalfactory = new LandAnimalFactory();
}
return animalfactory;
}
}
public class SeaAnimalFactory : AnimalFactory
{
public override IAnimal GetAnimal(string animalType)
{
IAnimal ianimal = null;
switch (animalType)
{
case "Shark":
ianimal = new Shark();
break;
case "Octopus":
ianimal = new Octopus();
break;
}
return ianimal;
}
}
public class LandAnimalFactory : AnimalFactory
{
public override IAnimal GetAnimal(string animalType)
{
IAnimal ianimal = null;
switch (animalType)
{
case "Cat":
ianimal = new Cat();
break;
case "Dog":
ianimal = new Dog();
break;
case "Lion":
ianimal = new Lion();
break;
}
return ianimal;
}
}
//****************End:Abstract Factory Pattern******************//
Another Example as below:-
//The DataType enumeration.
public enum DataType
{
File,
DB
}
//The Abstract Item class.
public interface IData
{
string GetData();
}
//The Abstract Factory interface.
public interface IDataFactory
{
IData CreateData();
}
//The Concrete Factory #1 class.
public class DataFromDB : IDataFactory
{
public IData CreateData()
{
//IMPLEMENT YOUR LOGIC
return new Data("DataBase source");
}
}
//The Concrete Factory #2 class.
public class DataFromFile : IDataFactory
{
public IData CreateData()
{
//IMPLEMENT YOUR LOGIC
return new Data("File source");
}
}
//The Item class.
public class Data : IData
{
private string _value;
public Data(string value)
{
_value = value;
}
public string GetData()
{
return _value;
}
}
//The Client class.
public class DataSource
{
private IData _data;
public DataSource(DataType dataType)
{
IDataFactory factory;
switch (dataType)
{
case DataType.File:
factory = new DataFromFile();
_data = factory.CreateData();
break;
case DataType.DB:
factory = new DataFromDB();
_data = factory.CreateData();
break;
}
}
public string GetDataFromSource()
{
return _data.GetData();
}
}
//The following code shows how to call the Abstract Factory from an application client:
DataSource ds = new DataSource(DataType.DB);
DataSource ds = new DataSource(DataType.File);
string result = ds.GetDataFromSource();

No comments:
Post a Comment