public class ConcreteService1: IService1
{
private IService2 _service2;
public void doSomething()
{
_service2 = new ConcreteService2();
.....
}
}
public class ConcreateService2: IService2
{
private IService1 _service1;
public void doSomething()
{
_service1 = new ConcreteService1();
.....
}
}
Is this is the situation that Factory or Abstract factory can be used here. If yes can you please provide an example.
In your case as you have two classes with two different interfaces then Factory really doesn’t apply here. you should use abstract factory as
its an object and which delegates the object initialization to its separate functions.
Abstract Factory Interface for your example goes here
Factory pattern is used generally when you have too many different object initialization implementing same interface or abstract class. The pattern suggest to have a Factory method and based on type of object need to be initialized it creates the specific object and return it.
It helps in two ways
1. Keeps all the initialization code at one place
2. It never returns the concrete class which makes it more extendable and manageable
For more information please see this links
Differences between Abstract Factory Pattern and Factory Method
http://en.wikipedia.org/wiki/Abstract_factory_pattern
http://www.codeproject.com/Articles/68670/The-Factory-Pattern
http://en.wikipedia.org/wiki/Factory_method_pattern