| Autor |
Mensagem |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 20/08/2004 15:46:30
|
popuser
Smalltalk
Membro desde: 20/08/2004 15:42:17
Mensagens: 2
Offline
|
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
|
|
|
 |
|
|
![[Post New]](/templates/default/images/icon_minipost_new.gif) 20/08/2004 16:07:08
|
vamorim
JavaEvangelist
![[Avatar]](/images/avatar/bd5af7cd922fd2603be4ee3dc43b0b77.jpg)
Membro desde: 21/11/2003 17:08:39
Mensagens: 481
Localização: Rio de Janeiro - RJ
Offline
|
O segredo é o encapsulamento.
Leia isso e isso aqui também.
|
Vinci Pegoretti Amorim
http://vinci.blog.br |
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 20/08/2004 17:51:23
|
popuser
Smalltalk
Membro desde: 20/08/2004 15:42:17
Mensagens: 2
Offline
|
Teria algum exemplo para ficar mais claro para mim
|
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 20/08/2004 23:02:49
|
Luca
Moderador
![[Avatar]](/images/avatar/17e62166fc8586dfa4d1bc0e1742c08b.jpg)
Membro desde: 06/09/2002 14:30:10
Mensagens: 5810
Localização: São Paulo/SP ou Paraty/RJ
Offline
|
Olá
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: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.
Program to interfaces, not classes. This decouples interfaces from their implementations. Using loose coupling between objects promotes flexibility. To gain maximum flexibility, declare instance variables and method parameters to be of the least specific type required.
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:
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.
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.
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.
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
This message was edited 1 time. Last update was at 18/03/2005 07:27:51
|
Dare Obasanjo (Program Manager at Microsoft)
"The folks I know from across the industry who have to build large scale Web services on the Web today at Google, Yahoo!, Facebook, Windows Live, Amazon, etc are using RESTful Web services. The only times I encounter someone with good things to say about WS-* is if it is their job to pimp these technologies or they have already "invested" in WS-* and want to defend that investment."
CEP, JMS, JMX e coisas afins (ou não)
http://lucabastos.blogspot.com/ |
|
|
 |
|
|