1

Topic: Interfaces where favor?

. Class Salutation is strictly fastened on structure and interface IMessageWriter implementation. private static void Main () {IMessageWriter writer = new ConsoleMessageWriter (); var salutation = new Salutation (writer); salutation. Exclaim ();} class Salutation {public Salutation (IMessageWriter writer) {...} public void Exclaim () {IMessageWriter. ChangeData ();... GetDataAndChangeIt (...);} } Today IMessageWriter. ChangeData (); - adds file recording, and tomorrow deletes? You change implementation of class IMessageWriter in class ConsoleMessageWriter, you change interface IMessageWriter (a title of methods/additions of new/order their call) and walk on the code with , test where that ceases to work as earlier. Freedom. Class Salutation does not depend on the "left" classes. Its methods strictly define "" with which help it is possible with them . Methods both worked today and will work tomorrow and always. Method Exclaim will always accept  parameter  data SomeData. private static void Main () {MessageWriter writer = new ConsoleMessageWriter (); SomeData data=to_some_data_converter (writter. GetData ()); var salutation = new Salutation (); salutation. Exclaim (data);} class Salutation {public void Exclaim (SomeData data) {... GetDataAndChangeIt (...);} } - - If someone tells that the logic of implementation IMessageWriter in class ConsoleMessageWriter will not change, because we of type at first program the abstract layer which moderates all our requirements in the future but only then we write its implementation is not truth, in due course it is necessary to change  all parts of the program. Since all in due course changes: iron, algorithms of search/enciphering, the requirement of customers. And it is impossible to predict all, so it is impossible to write the super-abstract model which in the future never should be changed or changed so that not to affect all parts of the program which implement it.

2

Re: Interfaces where favor?

From my point of view there are three applications to interfaces. 1. Plural implementations (but not in any light future, and here and now). Here, I think, all is clear. The case with testing (instead of dialogue with a DB is used the stub) though here it is possible to use different mock-frejmvorki here concerns and to do without interfaces, but it already other subject. 2. Different people parallely write different parts of system and the artifact on which interaction will be under construction is necessary to them. You write the interface and one it use, the second implement it. 3. As self-restriction. In a normal class to thrust a public method psychologically is easier, than in the interface. Then this method  where it is not necessary too is easier. And with the interface already two times you will think. But it, of course, not the technical argument, is faster to whom as more conveniently. As a whole personally I in  pascal very much liked the concept of sharing of a class on interface and implementation and in the modern languages of it does not suffice, . It is easy for imitating introduction of the interface for each class though litters the code. Though it can I such become outdated and always I look at the program text, instead of on any windows outline from IDE which show this interface automatically.

3

Re: Interfaces where favor?

Hello, #John, you wrote: J> - - J> If someone tells that the logic of implementation IMessageWriter in class ConsoleMessageWriter will not change, J> because we of type at first program the abstract layer which moderates all our requirements in the future but only then we write its implementation is not truth, in due course it is necessary to change  all parts of the program. Since all in due course changes: iron, algorithms of search/enciphering, the requirement of customers. And it is impossible to predict all, so it is impossible to write the super-abstract model which in the future never should be changed or changed so that not to affect all parts of the program which implement it. The stub is clear what to change interface implementation it is possible easily, including and with a view of sabotage. Too most it is possible to tell and about the abstract classes and other methods of sharing of the information and logic. But to change interfaces are a nasty taste, very nasty taste. And global protection against the fool is not present! , the main favor of interfaces at not to global development (type large SDK) not in protection against the fool, and first of all in possibility to build the abstract model of the task. To describe the future model for me personally interfaces faster and is easier, than classes. Classes, even abstract, it is already following level of performance of the task, they are closer to implementation. In them it is possible to use overlapping of methods, we tell. As a whole to use or not to use interfaces - a matter of taste. Most likely, usage of interfaces even slightly reduces speed of operation of the code (the 100-th shares of percent). But they add beauty. For me interfaces - it is simple templates of the future implementation of the task. P.S. And still, by experience they allow to organize rather easily interaction developers (information interchange) since oblige to the little, and describe enough!

4

Re: Interfaces where favor?

Hello, #John, you wrote: And in what a question? Yes, sometimes it would be desirable to change so that in available interfaces not to be laid down. But frequently it is required to change so what to be laid down in all of them it is possible. Depends on the task and what possibilities have been provided initially. The interface gives only signatures of methods - without implementation. You have more than freedom in implementation of the successor than if already there is a class with any implementation. J> MessageWriter writer = new ConsoleMessageWriter (); J> SomeData data=to_some_data_converter (writter. GetData ()); also it is possible to name somehow nevertheless objects according to their behavior? And that you followed an example where support of a different output is used use it for data acquisition. It, to put it mildly, confuses. The example which was initially yes, the signature writer will not change from an output to the console, in a file or even in the specific table of a DB. It is possible and not all cases, but nevertheless decent flexibility. Your example is not clear from a word absolutely. If class Salutation can be reduced to dependence on simple object - perfectly. But in Main at you all the same forms specific writer (which at you actually loader). But what if you should read out other data from other source? Or from this a source, but with the new version of a data format? How you will resolve? Quite convenient variant - certain basic classes/interfaces and classes implementing it which actually read out and return your object of a type data. Interfaces here it is more convenient that they "pure", are not present nested implementation which in the future it can appear only hindering for implementation of new successors. A basis of your example - it from the book "implementation of dependences in.NET", aha? Well so there then the author still for a long time and carefully tells about that in what an essence of dependences on abstractions instead of specific implementations. And it only the first example. And it, is remembered, makes a reservation. That interfaces are not mandatory, the abstract classes too suit.

5

Re: Interfaces where favor?

Hello, #John, you wrote: J> Today IMessageWriter. ChangeData (); - adds file recording, and tomorrow deletes? If such implementation this serious restriction why it is not displayed in the interface? For what in that case it generally should be fenced?

6

Re: Interfaces where favor?

Hello, IQuerist, you wrote: J>> Today IMessageWriter. ChangeData (); - adds file recording, and tomorrow deletes? IQ> if such implementation this serious restriction why it is not displayed in the interface? Basically, yes. Reflecting, came in to an output that in interfaces it is possible to describe only the methods which implementation will guarantee a state of the exterior data that they  same as well as to a method call. E.g. functions sin. IMath {float sin (float x);} Math_x86:IMath {float sin (float x) {...}} Counted, wrote down result in a cell of storage and everywhere where changed storage/registers for calculations returned in former values. So we guarantee that even if method implementation ` sin ` in the future to change, we should not run on the code and to look where this function is caused and to resolve a situation with that that function changes any data to which the subsequent code refers.

7

Re: Interfaces where favor?

Hello, #John, you wrote: If the class does not demand the interface and it is not necessary to spread it violently. It is better to ask a question how to test a class and methods, and from it it will be clear where the interface and where it is not necessary is necessary.