Static (common in VB.NET) or normal method

I want to know which one is preferable when coding to use static methods or regular instances, I prefer to use static methods if there are few of them, but if there were a lot of them I start to doubt

Example

EmployeeCollection EmpLst = EmployeeManager.GetAllEmployees();

      

or

EmployeeManager EmpMgr = new EmployeeManager();
EmployeeCollection EmpLst = EmpMgr.GetAllEmployees();

      

if EmployeeManager has many methods (chooses to delete updates) is it okay to make them static.

and if it was a regular instance. it won't be a disadvantage if the object is instantiated every time on purpose, if GetAllEmployees () is used heavily.

What's the best approach to use?

+2


source to share


3 answers


If you have a lot of static methods, I assume you are not following OOP principles. Static methods are useful as factory methods or as helper methods. But I would avoid building application designs on top of them.



+3


source


You might want to take a look at the factory and singleton patterns, which are creativity patterns conceived for this kind of thing. For your problem, I would suggest using a singleton, which provides a one-time object creation.

Abstract Factory



Singleton

(Links to dofactory.com)

+1


source


In the case of your GetEmployee method, I'm sticking with static.

I usually use static unless the method requires access to any instance states and instance methods if needed. This way I don't use instance methods unless the method requires instance state.

0


source







All Articles