Programar para interface

E ai pessoal, blz

Eu ja vi algumas pessoas afirmarem que uns dos mandamentos de OOP eh programar para "interface" e nao para "classe". Entao gostaria de saber, o porq desta afirmacao

O segredo é o encapsulamento.

Leia isso e isso aqui também.

Teria algum exemplo para ficar mais claro para mim

Olá

[quote=“vamorim”]O segredo é o encapsulamento.
Leia isso e isso aqui também.[/quote]

Vinci, não perca seu tempo lendo linhadecódigo. Use para ler o Effective Java do Joshua Bloch e então terá uma noção do que é programar para interfaces. Sugiro que leia o capítulo 4 e depois o capítulo 7. Se quiser um atalho para um trecho do capítulo 7 leia Item 34: Refer to objects by their interfaces.

Uma outra excelente alternativa é ler Rod Johnson, J2EE Design and Development. Este livro é tão bom que faço questão de transcrever todo um tópico. Lá no capítulo 4 página 115 está escrito:[list]Achieving Loose Coupling with Interfaces

The “first principle of reusable object-oriented design” advocated by the classic Gang of Four design patterns book is: “Program to an interface, not an implementation”. Fortunately, Java makes it very easy (and natural) to follow this principle.

Using interface-based architecture is particularly important in J2EE applications, because of their scale. Programming to interfaces rather than concrete classes adds a little complexity, but the rewards far outweigh the investment. There is a slight performance penalty for calling an object through an interface, but this is seldom an issue in practice.

A few of the many advantages of an interface-based approach include:

:arrow: The ability to change the implementing class of any application without affecting calling code. This enables us to parameterize any part of an application without breaking other components.

:arrow: Total freedom in implementing interfaces. There’s no need to commit to an inheritance hierarchy. However, is still possible to achieve code reuse by using concrete inheritance in interface implementations.

:arrow: The ability to provide simple test implementations and stub implementations of application interfaces as necessary, facilitating the testing of other classes and enabling multiple teams to work in paralel after they have agreed on intefaces.

Adopting interface-based architecture is also the best way to ensure that a J2EE application is portable, yet is able to leverage vendor-specific optimizations and enhancements.[/list]
O item seguinte do livro é igualmente importante e trata do segundo princípio de OOD enfatizado pelo livro da GoF: Favor object composition over class inheritance. Bem, por aí vai o livro. É leitura quase obrigatória para quem gosta de fazer as coisas direitinho.

popuser, garanto que Joshua Bloch e Rod Johnson respondem bem melhor a sua pergunta do qualquer dos links citados. Se você mora perto de SP não perca o tutorial do Paulo Silveira na manhã do próximo dia 28. Aliás este chamado vale para todos.

[]s
Luca