Interfaces vs Public Class Members

I noticed that some programmers like to create interfaces for almost all of their classes. I like interfaces for certain things (like checking if an object supports certain behavior and then has an interface for that behavior), but excessive use of interfaces can sometimes inflate code. When I declare methods or properties publicly available, I would expect people to just use my specific classes, and I really don't understand the need to create interfaces on top of this.

I would like to hear your interfaces. When do you use them and for what purpose?

Thank.

+3
source share
8 answers

Applying any design template or idea without thought, simply because someone told you that this is good practice, it is a bad idea.

This means that you create a separate interface for each class you create. You should at least be able to give a good reason for every design decision, and “because Joe says it's good practice” is not a good enough reason.

. , , . ; , , , " " .

+6

- . () . ; , , - ? .

+3

.

, , , - , .

+1

.

, ( ) IThing ThingFactory.

IThing ( ConcreteThing). Factory.

ThingFactory.CreateThing( ).

, AmericanConcreteThing. , . , -, .

EuropeanThing, TexasAmericanThing - .

, , :

ThingFactory.CreateThing( ) TexasAmericanThing: IThing.

, ThingFactory,

public static IThing CreateThing(Account a)
{ 
   return new AmericanThing();
}

to 

public static IThing CreateThing(Account a)
{
   if (a.State == State.TEXAS) return new TexasAmericanThing();
   return new AmericanThing();
}
+1

. , , . . , . .

. . . , , , .

OTOH, , , , . , , , . ! . , , , , .

+1

IoC .

0

, . , , , , , , . , .

0

:
* //

* (/)

0

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


All Articles