良好的源代码可以让我在 C# 上的工厂设计模式方面取得先机

发布于 2024-11-26 23:33:53 字数 1040 浏览 2 评论 0原文

我觉得我应该开始在我的一些代码上使用工厂方法设计模式。这就是我正在做的事情;

下面的代码是我的 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

    }

}

我也将为 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
    }
}

因此,这将一次又一次地重复。

我尝试为此创建一些工厂模式,但所有抽象类都是混合的,我很困惑(我认为这是因为这是我第一次尝试做类似的事情)。

任何人都可以帮助我解决这个问题并为我指出可以阅读和理解的良好源代码吗?

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:

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅气称霸 2024-12-03 23:33:53

我认为 这篇关于 AbstractFactoryPattern 的代码项目文章 很好地提供了有用的例子。

但是,除非您有充分的理由,否则不应在多个不同的命名空间中创建相同的类。您始终可以使用 using Accom; 从 Traf 命名空间访问您的 Generator 类。


编辑以回应评论,不同命名空间中的每个生成器将具有一组不同的方法。

如果实现具有不同的方法,则不能使用抽象工厂模式。抽象工厂模式的思想是创建一个通用接口,工厂返回的所有对象都将实现该接口,然后使用工厂中的某些上下文为给定情况选择正确的实现。

通过使用工厂获得的优势称为控制反转。基本上,您的客户端代码不依赖于 Generator 类的特定实现(通过拥有该类型的变量,或通过为其调用构造函数)。

但是,如果您需要访问特定于实现的方法,那么您无法通过公共接口访问它们,这意味着您无法获得控制反转的好处,这意味着没有真正的理由使用抽象工厂模式。

I think this code project article on the AbstractFactoryPattern does a pretty good job of providing a useful example.

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; to access your Generator class from the Traf namespace.


Edit in response to comment that each Generator in a different namespace will have a different set of methods.

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).

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.

甜柠檬 2024-12-03 23:33:53
  • 为生成器创建一个抽象类,
  • 为不同类型的生成器创建子类
  • 创建一个工厂类
  • 将工厂设置为单例
  • 创建一个方法(通用),该方法将参数作为字符串(如果类位于不同的命名空间中,则使用命名空间)
  • 使用反射创建方法中的不同对象
  • 从公共返回基类型
  • 创建不同的方法(A,B,C)来获取不同的实例,调用方法(公共)
  • 将结果从公共转换为你想要返回的类型

编辑

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"
    }
}
  • 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)
  • cast the result from common to type u want to return

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"
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文