What are the disadvantages of using a static property or method in an OO approach?

I need to explain why I do not use static methods / propertis. For example,

String s=String.Empty;

Is this property (owned by the .Net framework) incorrect? should be like?

String s= new EmptySting();

or

IEmptyStringFactory factory=new EmptyStringFactory();

String s= factory.Create();
+3
source share
9 answers

I think the worst thing about using statics is that you may encounter tight coupling between classes. See ASP.NET before System.Web.Abstractions. This makes your classes more difficult to test and possibly more prone to errors causing problems on the system.

+7
source

, ? .

, , , , .

( "" string.Empty, , .)

+9

. , .


String s=String.Empty;

. , , - . , , "" . , . , , .


String s= new EmptySting();

. , . , . string.Empty, singleton, .


IEmptyStringFactory factory=new EmptyStringFactory();
String s= factory.Create();

, , . , , , , .

, , (, ). , , , factory (, StringFactory.CreateEmpty() StringFactory.Create("foo")). , factory , .

+2

, String.Empty ( Math.PI Math.E) . .

() , "":

, , , - , #.

- , , factory - (, Int.TryParse).

, "", , , .

, /, , -, , . , , . Math , . Math , Math, , .

, :

Math mD = new Math(AngleMode.Degrees); // ooooh, use one with degrees instead
double x = mD.Sin(angleInDegrees);

:

double x = Math.Sin(angleInDegrees * Math.PI / 180);

(, , ).

, , . - .

( , . ).

+2

, - , . String.Empty "", , .

+1

, - , "".

  • //

, , . System.String , .

, . , , . mocks , , , . ( , , , ).

- o.k. , .

+1

OO OO, . - , . , , , , .

.

, , ? , , - , , . - , .

, SqlConnection, , . , , . , , .

: - , . , , , mixin. ; .

+1

:

  • Statics .
  • ( , , , )
  • , .

  • , , .
  • Eeerm, .
+1

, , : , . , . .ctor.

, . . Account. , , . . Account? account.ConvertTo(Currency.Euro)? , ? CurrencyConverter.Convert( , Currency.Euro)? , , .

0

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


All Articles