Should we ignore the creation of objects for individual classes?

I play with design templates and everything is going well. One thing I'm not sure about is whether to abstract the creation of an object when there is currently only one object?

For example, the project I'm working on contains two different types of user that are not connected in any way. These are IStudent and IStaff. There will never be any other types of users for the application in question (Role employees handle all non-student interactions with the system).

In my controllers, I could simply:

IStudent student = new Student(); 

Or am I something like this:

 public static class UserFactory { public static T Create<T>() where T : class { if(typeof(T) == typeof(IStudent)) return new Student() as T; if (typeof(T) == typeof(IStaff)) return new Staff() as T; throw new Exception("The requested user type is not valid."); } } 

And then:

 IStudent student = UserFactory.Create<IStudent>(); 

Is this an excess? I am trying to develop best practices in these situations.

+7
source share
5 answers

Personally, I use TDD for most developments. One thing that I like about this is that it gives an answer to your question: "unless you need to skip the unit test pass."

In other words, if you do not need it, then do not do it.

+4
source

Ok ... you need a factory class, or just you need to call the constructor. Why are people afraid to call a constructor?

I think the templates are good. I also believe that their abuse is pure evil :)

If you begin to realize that your programming paradigm makes your life more complicated, discard that paradigm, not your programmer’s ability. Once too much abstraction is simply not useful.

Being clean and writing clean codes does not mean following the pattern, which means that the write code makes sense.

Factory pattern: if we need to write factory for factory and factory for factory for factory .... until the deadline comes when we write the real code?

Since I see system analysts more as architects than masons, I believe that only after some rule cannot make your software good.

People can blame Turing or the Church or Goedel if they want to, but not their fault if the software that writes the software cannot be written :) We still need our human part to write the software, our creativity and imagination and of our soul, also, if any software developer tried to make it a pure mechanical action, programming is still an art in a large part of it.

Conclusion: I think that templates are very good if they are used with the right criticism and always follow a good programmer in the sixth sense :)

I think that programming requires some flexibility, we are not in an ideal world, we do not have an ideal computer, we are not perfect, and our software cannot be perfect, and especially a machine cannot think, still.

So, calling a constructor is always better than 2,000 lines of code to just call the constructor.

+4
source

In this particular case, your solution does not provide benefits with a simple constructor call. If you have a reason for the factory, then by all means. Otherwise, do not reinstall your code.

If your controller always knows which user it is going to create (i.e., it will always go through a specific type in your type parameter), you do not need a factory.

+2
source

Usually, you use the factory pattern when creating more than one type, and you want to hide the creation to prevent ugly things like:

 if(newPerson == "student") person = new Student(); else if(newPerson == "Staff") person = new Staff(); 

You could just do:

 Person.CreatePerson(newPerson); 

Again, this only matters if you need to do it, if you do it in several places.

+2
source

I would say that your message:

 there will never be any other types of user (staff Roles handle all none-student interactions with the system) 

Very similar to this:

 640K of memory is all that anybody with a computer would ever need 

In other words, use simpler code as suggested by the Sounders, but keep in mind that one day something can change, even if today it seems impossible.

EDIT

If you have a sequence of types (not only 2) of completely unrelated classes (from a design point of view), the Factory template that you apply can be quite appropriate IMHO.

Good luck.

+1
source

All Articles