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?
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.
- 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"
}
}
精彩评论