Documente Academic
Documente Profesional
Documente Cultură
http://www.artima.com/lejava/articles/designprincip...
Articles | News | Weblogs | Buzz | Books | Forums Leading-Edge Java | Discuss | Print | Email | Screen Friendly Version | Previous | Next Sponsored Link
Leading-Edge Java
Summary
In this interv iew, Erich Gam m a, co-author of the landm ark book, Design Patterns, talks with Bill Venners about two design principles: program to an interface, not an im plem entation, and fav or object com position ov er class inheritance. Erich Gam m a lept onto the software world stage in 1 9 9 5 as co-author of the best-selling book Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley , 1 9 9 5) [1 ]. This landm ark work, often referred to as the Gang of Four (GoF) book, cataloged 2 3 specific solutions to com m on design problem s. In 1 9 9 8, he team ed up with Kent Beck to produce JUnit [2 ], the de facto unit testing tool in the Jav a com m unity . Gam m a currently is an IBM Distinguished Engineer at IBM's Object Technology International (OTI) lab in Zurich, Switzerland. He prov ides leadership in the Eclipse com m unity , and is responsible for the Jav a dev elopm ent effort for the Eclipse platform [3 ]. On October 2 7 , 2 004 , Bill Venners m et with Erich Gam m a at the OOPSLA conference in Vancouv er, Canada. In this interv iew, which will be published in m ultiple installm ents in Leading-Edge Java on Artim a Dev eloper, Gam m a giv es insights into software design. In Part I: How to Use Design Patterns, Gam m a describes giv es his opinion on the appropriate way s to think about and use design patterns, and describes the difference between patterns libraries, such as GoF, and an Alexandrian pattern language. In Part II: Erich Gam m a on Flexibility and Reuse, Gam m a discusses the im portance of reusability , the risks of speculating, and the problem of fram eworkitis. In this third installm ent, Gam m a discusses two design principles highlighted in the GoF book: program to an interface, not an im plem entation, and fav or object com position ov er class inheritance.
1 de 9 13-01-2011 21:28
http://www.artima.com/lejava/articles/designprincip...
2 de 9
13-01-2011 21:28
http://www.artima.com/lejava/articles/designprincip...
IMarkerResolution2 or IWorkbenchPart2 in our APIs. These interfaces add m ethods to the base interfaces IMarkerResolution and IWorkbenchPart. Since the additions
are done in separate extension interfaces y ou do not break the clients. Howev er, there is now som e burden on the caller in that they need to determ ine at run- tim e whether an object im plem ents a particular extension interface. Another lesson learned is that y ou should focus not only on dev eloping v ersion one, but to also think about the following v ersions. This doesn't m ean designing in future extensibility , but just keeping in m ind that y ou hav e to m aintain what y ou produce and try to keep the API stable for a long tim e. You want to build to last. That's been an im portant them e of Eclipse dev elopm ent since we started. We hav e built Eclipse as a platform . We alway s keep in m ind as we design Eclipse that it has to last ten or twenty y ears. This can be scary at tim es. We added support for ev olution in the base platform when we started. One exam ple of this is the IAdaptable interface. Classes im plem enting this interface can be adapted to another interface. This is an exam ple of the Extension Object pattern,. [4 ] Bill Venners: It's funny nowaday s we're so m uch m ore adv anced, but when we say build to last, we m ean ten or twenty y ears. When the ancient Egy ptians built to last, they m eant... Erich Gamma: Thousands of y ears, right? But for Eclipse, ten to twenty y ears, wow. Quiet honestly , I don't env ision a software archeologist finding an Eclipse installation stored som ewhere on a hard disk in ten or twenty y ears. I really m ean that Eclipse should still be able to support an activ e com m unity in ten or twenty y ears.
3 de 9
13-01-2011 21:28
http://www.artima.com/lejava/articles/designprincip...
Bill Venners: I guess the core of m y question about "program to an interface, not to an im plem enation," is this: In Jav a, there's a special kind of class called interface that if I'm writing I put in code fontthe Jav a interface construct. But then there's the object-oriented interface concept, and ev ery class has that object-oriented interface concept. If I'm writing client code and need to use an object, that object's class exists in som e ty pe hierarchy . At the top of the hierarchy , it's v ery abstract. At the bottom , it's v ery concrete. The way I think about program m ing to interfaces is that, as I write client code, I want to write against the interface of the ty pe that's as far up that hierarchy as I can go, without going too far. Ev ery single one of those ty pes in the hierarchy has a contract. Erich Gamma: You're right. And, writing against a ty pe far up in the hierarchy is consistent with the program m ing to an interface principle. Bill Venners: How can I write to an im plem entation? Erich Gamma: Im agine I define an interface with fiv e m ethods, and I define an im plem entation class below that im plem ents these fiv e m ethods and adds another ten m ethods. If only the interface is published as API then if y ou call one of these ten m ethods y ou m ake an internal call. You call a m ethod that is out of contract, which I m ight break any tim e. So it's the difference, as Martin Fowler would say , between public and published. Som ething can be public, but that doesn't m ean y ou hav e published it. In Eclipse we hav e the nam ing conv ention that a package which includes the segm ent "internal" identifies internal packages. They contain ty pes which we do not consider published ty pes ev en when the package includes a public ty pe. So the nice short package nam e is for API and the long nam e is for internals. Obv iously using package priv ate classes and interfaces is another w ay to hide im plem entation ty pes in Jav a. Bill Venners: Now I understand what y ou m ean. There's public and published. Martin Fowler has nice term s for that difference. Erich Gamma: And in Eclipse we hav e the conv entions for that difference. Actually we ev en hav e tool support. In Eclipse 3 .1 we hav e added support for defining rules for which packages are published API. These access rules are defined on a project's class path. Once y ou hav e these access restrictions defined the Eclipse Jav a dev elopm ent tools report access to internal classes in the sam e way as any other com piler warnings. For exam ple y ou get feedback as y ou ty pe when y ou add a dependency to a ty pe that isn't published. Bill Venners: So if I write code that talks to the interface of a non-published class, that's a way I am writing to the im plem entation, and it m ay break. Erich Gamma: Yes, and the explanation from the angle of the prov ider is that I
4 de 9
13-01-2011 21:28
http://www.artima.com/lejava/articles/designprincip...
need som e freedom and reserv e the right to change the im plem entation.
5 de 9
13-01-2011 21:28
http://www.artima.com/lejava/articles/designprincip...
Bill Venners: So it sounds like thinking about interfaces becom es m ore im portant as the project scales up. If the project is just two or three people, it is not quite as im portant to think about the interfaces, because if y ou need to change them y ou change them . The refactoring support tools... Erich Gamma: ... will do it all for y ou. Bill Venners: But if it is a 1 00-person team , that m eans people will be partitioned into groups. Different groups will hav e different areas of responsibility . Erich Gamma: A practice that we follow is to assign a com ponent to a group. The group is responsible for the com ponent and publishes its API. Dependencies are then defined through API. We also resist the tem ptation to define friend relationships, that is, where som e com ponents are m ore equal than others and are allowed to use internals. In Eclipse all com ponents are equal. For exam ple, the Jav a dev elopm ent tool plug-ins hav e no special priv ileges and use the sam e APIs as all other plug-ins. Once y ou hav e published an API then it is y our responsibility to keep them stable. Otherwise y ou will break the other com ponents and nobody is able to m ake progress. Hav ing stable APIs is a key to m aking progress in a project of this size. In a closed env ironm ent as y ou hav e described it y ou hav e m ore flexibility when it com es to m aking changes. For exam ple, y ou can use the Jav a deprecation support to allow other team s to gradually catch-up with y our changes. In such an env ironm ent y ou can rem ov e deprecated m ethods after som e agreed on tim e-interv al. This m ight not be possible in a fully exposed platform . There deprecated m ethods cannot be rem ov ed since y ou m ay still break a client som ewhere.
6 de 9
13-01-2011 21:28
http://www.artima.com/lejava/articles/designprincip...
m isunderstanding is that com position doesn't use inheritance at all. Com position is using inheritance, but ty pically y ou just im plem ent a sm all interface and y ou do not inherit from a big class. The Jav a listener idiom is a good exam ple for com position. With listeners y ou im plem ent a listener interface or inherit from what is called an adapter. You create a listener object and register it with a Button widget, for exam ple. There is no need to subclass Button to react to ev ents. Bill Venners: When I talk about the GoF book in m y design sem inar, I m ention that what shows up ov er and ov er is m ostly using com position with interface inheritance for different reasons. By interface inheritance I m ean, for exam ple, inheriting from pure v irtual base classes in C+ + , or code font interface inheritance in Jav a. The Listener exam ple y ou m ention, for instance, has inheritance going on. I im plem ent MouseListener to m ake MyMouseListener. When I pass an instance to a JPanel v ia addMouseListener, now I'm using com position because the front-end JPanel that's holding onto that MouseListener will call its mouseClicked m ethod. Erich Gamma: Yes, y ou hav e reduced the coupling. In addition y ou now hav e a separate listener object and y ou m ight ev en be able to connect it with other objects. Bill Venners: That extra flexibility of com position ov er inheritance is what I'v e observ ed, and it's som ething I'v e alway s had difficulty explaining. That's what I was hoping y ou could capture in words. Why ? What is really going on? Where does the increased flexibility really com e from ? Erich Gamma: We call this black box reuse. You hav e a container, and y ou plug in som e sm aller objects. These sm aller objects configure the container and custom ize the behav ior of the container. This is possible since the container delegates som e behav ior to the sm aller thing. In the end y ou get custom ization by configuration. This prov ides y ou with both flexibility and reuse opportunities for the sm aller things. That's powerful. Rather than giv ing y ou a lengthy explanation, let m e just point y ou to the Strategy pattern. It is m y prototy pical exam ple for the flexibility of com position ov er inheritance. The increased flexibility com es from the fact that y ou can plug-in different strategy objects and, m oreov ers, that y ou can ev en change the strategy objects dy nam ically at run-tim e. Bill Venners: So if I were to use inheritance... Erich Gamma: You can't do this m ix and m atch of strategy objects. In particular y ou cannot do it dy nam ically at run-tim e.
Next week
Com e back Monday , June 1 3 th for the next installm ent of this conv ersation with Erich Gam m a. If y ou'd like to receiv e a brief weekly em ail announcing new articles at Artim a Dev eloper, please subscribe to the Artim a Newsletter.
7 de 9
13-01-2011 21:28
http://www.artima.com/lejava/articles/designprincip...
Talk back!
Hav e an opinion about the design patterns topics discussed in this article? Discuss this article in the Articles Forum topic, Design Principles from Design Patterns.
Resources
[1 ] Erich Gam m a is co-author of Design Patterns: Elements of Reusable Object-Oriented Software, which is av ailable on Am azon.com at: http://www.am azon.com /exec/obidos/ASIN/02 01 6 3 3 6 1 2 / [2 ] Erich Gam m a is co-creator of JUnit, the defacto standard Jav a unit testing tool: http://www.junit.org/index.htm [3 ] Erich Gam m a leads the Jav a dev elopm ent effort for the Eclipse tool platform : http://www.eclipse.org/ [4 ] See "Extension Object," in Robert Martin, Pattern Languages of Program Design 3. Addison- Wesley , 1 9 9 7 , av ailable on Am azon.com at: http://www.am azon.com /exec/obidos/ASIN/02 01 3 1 01 1 2 / [5] "Ev olv ing Jav a-based APIs," by Jim des Riv ires: http://eclipse.org/eclipse/dev elopm ent/jav a-api-ev olution.htm l [See also] Contributing to Eclipse: Principles, Patterns, and Plug-I ns, by Erich Gam m a and Kent Beck, is av ailable on Am azon.com at: http://www.am azon.com /exec/obidos/ASIN/03 2 1 2 057 58/
Copyright 1996-2009 Artima, Inc . All Rights Reserved. - Privac y Polic y - T erms of Use - Advertise with Us
8 de 9
13-01-2011 21:28
http://www.artima.com/lejava/articles/designprincip...
9 de 9
13-01-2011 21:28