1
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();
 .....
 }

}

这是可以在这里使用工厂或抽象工厂的情况吗?如果是,请您举个例子。

4

1 回答 1

0

在您的情况下,因为您有两个具有两个不同接口的类,所以 Factory 在这里真的不适用。您应该使用抽象工厂作为它的对象,并将对象初始化委托给其单独的函数。

您的示例的抽象工厂接口在这里

public Interface IAbstractInterfaceFactory
{

IService1 InitializeService1();
IService2 InitializeService2();

}

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

于 2013-02-06T01:24:04.863 回答