简体   繁体   English

好的源代码可以使我在C#上的工厂设计模式上有所作为

[英]Good source code that could gives me head start on factory design pattern on c#

I feel that I should start using the factory method design pattern on some of my code. 我觉得我应该在某些代码上开始使用工厂方法设计模式。 Here is what I am doing; 这就是我在做什么;

The below code is my generator class for Accom namespace: 以下代码是我的Accom名称空间的生成器类:

namespace Accom {

    public class Generator {

      int? _id;
      string _name;

      public Generator(int? id = null, string name = null) {
          _id = id;
          _name = name;
      }

      public string GetBaseUrl() { 

          //some logic sits here.

          return somestring;

      }

      //Can have some other methods as well

    }

}

I will have the same start-up for Traf namespace as well: 我也将使用相同的Traf命名空间启动:

namespace Traf {
    public class Generator {
      int? _id;
      string _name;

      public Generator(int? id = null, string name = null) {
          _id = id;
          _name = name;
      }

      public string GetBaseUrl() { 
          //some logic sits here. Same one with the Accom.

          return somestring;
      }

      //Can have some other methods as well
    }
}

So, this will repeat again and again. 因此,这将一次又一次地重复。

I tried to create some factory pattern for that but all of the abstract classes were mixed and I confused a lot (I think it is because this is first time I am trying to do something like that). 我试图为此创建一些工厂模式,但是所有抽象类都混杂了,我感到很困惑(我认为这是因为这是我第一次尝试做类似的事情)。

Can anyone help me on this and point me to good source code that I can read and get a sense from? 谁能在这方面为我提供帮助,并为我提供可以阅读并从中获得启发的优质源代码?

  • Create an abstract class for generator 为生成器创建一个抽象类
  • create child class for different types of generator 为不同类型的生成器创建子类
  • Make a factory class 上工厂课
  • Make factory as singleton 使工厂成为单例
  • Create one method (Common) which will take parameter as string (with namespace if classes are in different namespace ) 创建一个将参数作为字符串的方法(通用)(如果类在不同的名称空间中,则使用名称空间)
  • Use reflection to create instance of different object in method common 使用反射在通用方法中创建不同对象的实例
  • return base type from common 从公共返回基本类型
  • Create different methods (A,B,C) for get different instance, call method (Common) 创建不同的方法(A,B,C)以获取不同的实例,调用方法(公共)
  • cast the result from common to type u want to return 将结果从common转换为要返回的类型

EDIT 编辑

public abstract class Generator
{
    public Generator(int? i, string name) { }
    public abstract string GetBaseUrl();
}

public class GeneratorA : Generator
{
    public GeneratorA(int? i, string name) : base(i, name) { }
}
public class GeneratorB : Generator
{
    public GeneratorB(int? i, string name) : base(i, name) { }
}
public class GeneratorFactory
{
    // Make singleton
    public GeneratorB GenerateB(int? i, string name)
    {
        return (GeneratorB)this.Generate(i, name, "GeneratorB");
    }

    public GeneratorA GenerateA(int? i, string name)
    {
        return (GeneratorA)this.Generate(i, name, "GeneratorA");
    }

    public Generator Generate(int? i, string name, string genType)
    {
        return new GeneratorA(); // use reflection to generate child generator based on string "genType"
    }
}

I think this code project article on the AbstractFactoryPattern does a pretty good job of providing a useful example. 我认为AbstractFactoryPattern上的此代码项目文章在提供有用的示例方面做得很好。

However, unless you have a really good reason to, you should not create the same class in several different namespaces. 但是,除非有充分的理由,否则不应在多个不同的命名空间中创建相同的类。 You can always use using Accom; 您始终可以using Accom; to access your Generator class from the Traf namespace. 从Traf名​​称空间访问Generator类。


Edit in response to comment that each Generator in a different namespace will have a different set of methods. 编辑以回应评论,即不同名称空间中的每个Generator将具有不同的方法集。

You can't use the abstract factory pattern if the implementations will have different methods. 如果实现将具有不同的方法,则不能使用抽象工厂模式。 The idea of the abstract factory pattern is to create a common interface that all objects returned by the factory will implement, then use some context in the factory to choose the correct implementation for a given situation. 抽象工厂模式的想法是创建一个通用接口,工厂将返回的所有对象都将实现该接口,然后在工厂中使用某些上下文为给定情况选择正确的实现。

The advantage you gain by using a factory is called Inversion of Control . 通过使用工厂获得的优势称为控制反转 Basically, your client code does not depend on a particular implementation of the Generator class (by having a variable of that type, or by calling a constructor for it). 基本上,您的客户端代码不依赖于Generator类的特定实现(通过具有该类型的变量或通过为其调用构造函数)。

However, if you need to access methods that are specific to an implementation, then you can't access them through a common interface, which means you don't gain the Inversion of Control benefit, which means there is no real reason to use the abstract factory pattern. 但是,如果您需要访问特定于实现的方法,则无法通过公共接口访问它们,这意味着您无法获得“控制反转”的好处,这意味着没有真正的理由使用抽象工厂模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM