良好的源代码可以让我在 C# 上的工厂设计模式方面取得先机
我觉得我应该开始在我的一些代码上使用工厂方法设计模式。这就是我正在做的事情;
下面的代码是我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 这篇关于 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.
编辑
EDIT