Factory Implementing a Template in C #

I am implementing a factory template that looks like this.

public class FeedFactory { #region Singleton Pattern //.. #endregion private static Feed[] _factory = new Feed[(int)FeedType.Total]; public void RegisterFeed(FeedType feedType,Feed feed) { if (_factory[(int)feedType] == null) { _factory[(int)feedType] = feed; } else { // already registered } } public Feed GetFeed(FeedType feedType) { return _factory[(int)feedType]; } } 

Here Feed is an abstract class from which different classes are inherited. How can I register different classes? Is it possible to do this from your constructor?

+4
source share
5 answers

This is not a factory template. A factory will always contain some design logic, at least one new . What a factory idea: the caller does not need to worry about how objects are created. This is a single user repository.

So, first of all, instead of using an array, you should have a dictionary with an indexed type.

 private static Dictionary<Type, Feed> _singletons = new Dictionary<Type, Feed>(); 

After that, you do not need a registration method. The dictionary should be filled automatically when receiving single numbers.

Now I assume that your Feed class has a default constructor with no arguments. In this case, you can implement the factory method directly from the abstract Feed class. We are going to use some generics here because it allows you to manage inheritance:

 public abstract class Feed { public static T GetInstance<T>() where T:Feed, new() { T instance = new T(); // TODO: Implement here other initializing behaviour return instance; } } 

Now back to your singleton repository.

 public class FeedSingletonRepository { private static readonly object _padlock = new object(); private static Dictionary<Type, Feed> _singletons = new Dictionary<Type, Feed>(); public static T GetFeed<T>() where T:Feed { lock(_padlock) { if (!_singletons.ContainsKey(typeof(T)) { _singletons[typeof(T)] = Feed.GetInstance<T>(); } return (T)_singletons[typeof(T)]; } } } 

Please note that I have included streaming behavior, which is good to do when you work with single games.

Now, if you want to get a singleton for a given type inheriting from Feed (call it SpecializedFeedType ), all you have to do is:

 var singleton = FeedSingletonRepository.GetFeed<SpecializedFeedType>(); 

or

 SpecializedFeedType singleton = FeedSingletonRepository.GetFeed(); 

which is the same line with a slightly different syntax.

Edit2: some syntax errors have changed.

+7
source

Just as the side of the note is how the factory is designed to wrap the creation, a curious choice is that you create objects and register them with the factory. Is this more likely an object repository than a factory, or is it more for the class than I see?

If it's a repository of objects, you can also find additional inspiration on other issues, like this one .

+6
source

When you call the RegisterFeed method, you need to pass a specific instance of the Feed class. Therefore, the caller is responsible for the specific implementation.

+1
source

Just register the type of classes you want to create, then use Activator.CreateInstance to create instances of this type.

It should work as follows:

 private static Type[] _factory = new Type[(int)FeedType.Total]; public void RegisterFeed(FeedType feedType, Type type) { ... _factory[(int)feedType] = type; ... } public Feed GetFeed(FeedType feedType) { return Activator.CreateInstance(_factory[(int)feedType]) as Feed; } 

You can call RegisterFeed as follows:

 RegisterFeed(FeedType.SomethingSpecial, typeof(MyDerivedSpecialFeed)); 
+1
source
 class FeedFactory { public IFeedFactory GetFeedFactory(string type) { switch(type) { case "1": return new Feed1(); break; case "2": return new Feed2(); break; } } } 

Please note that all feeds must implement the IFeedFactory interface and implement the required method.

// From the client

 FeedFactory ff1 = new FeedFactory(); IFeedFactory obj = ff1.GetFeedFactory("1"); obj.ExecuteMethod(); 
0
source

Source: https://habr.com/ru/post/1332877/


All Articles