Mutable Capabilities
←Older revision | Revision as of 07:06, 9 June 2013 | ||
Line 43: | Line 43: | ||
You are designing an API for an object whose capabilities are actually mutable. Listening for a particular type in a Lookup is much less code, and much clearer, than defining a bunch of event types, listener classes and addThisListener(), addThatListener(). Example: In the Java Card modules, there is a class Card. A Card has a lookup. Now a card might be a physical device plugged into your computer. Or it might be a virtual card definition used by an emulator for testing. A | You are designing an API for an object whose capabilities are actually mutable. Listening for a particular type in a Lookup is much less code, and much clearer, than defining a bunch of event types, listener classes and addThisListener(), addThatListener(). Example: In the Java Card modules, there is a class Card. A Card has a lookup. Now a card might be a physical device plugged into your computer. Or it might be a virtual card definition used by an emulator for testing. A | ||
- | virtual card has capabilities like Stop, Start and Resume. When you call StartCapability.start(), the StartCapability disappears from the Card's lookup and a StopCapability appears. But if it is a physical card, Start and Stop make no sense whatsoever - so for a real card they are not there. Other capabilities, such as PortProvider, which will tell you what TCP ports to use to send code to, attach a debugger to, etc., are present for both virtual cards and some real cards, if HTTP is the mechanism to deploy code to them - but other cards may have you run a native executable to deploy code and use no ports. So PortProvider is another optional capability. | + | virtual card has capabilities like Stop, Start and Resume. When you call StartCapability.start(), the StartCapability disappears from the Card's lookup and a StopCapability appears. But if it is a physical card, Start and Stop make no sense whatsoever - so for a real card they are not there. Other capabilities, such as PortProvider, which will tell you what TCP ports to use to send code to, attach a [[debugger]] to, etc., are present for both virtual cards and some real cards, if HTTP is the mechanism to deploy code to them - but other cards may have you run a native executable to deploy code and use no ports. So PortProvider is another optional capability. |
Note that you can add typing to [[Lookup]]-based [[API]]s if you find it useful or it makes your API easier (with Find Usages or [[Javadoc]]) to use. In org.netbeans.modules.javacard.spi.Card, in fact, there is | Note that you can add typing to [[Lookup]]-based [[API]]s if you find it useful or it makes your API easier (with Find Usages or [[Javadoc]]) to use. In org.netbeans.modules.javacard.spi.Card, in fact, there is | ||
Line 52: | Line 52: | ||
Perhaps there are other scenarios that I have missed for when Lookup makes sense, but I think those 4 cover most of the typical cases. If you're not doing something like that - if using Lookup adds complexity to your code without adding needed flexibility or future-proofing - then it's the wrong tool for the job. | Perhaps there are other scenarios that I have missed for when Lookup makes sense, but I think those 4 cover most of the typical cases. If you're not doing something like that - if using Lookup adds complexity to your code without adding needed flexibility or future-proofing - then it's the wrong tool for the job. | ||
- | |||
=== Overuse of [[Lookup]] === | === Overuse of [[Lookup]] === | ||
Sometimes people call [[Lookup]] ''magical bag''. The pejorative flavor of the nickname is based on observation that you can put your hand in it and pray to find the object you are seeking for. Sometimes people believe there shall be a better way. Yes, in many cases there is. [[Lookup]] is not handy for everything. It is basically a tool to implement a [[teleinterface]]s. In case a [[teleinterface]] is not what you want (like in case of [[CumulativeFactory]]), you'd rather prefer type safety and design proper [[API]] without ''magical bag'' - aka [[Lookup]]. More about that in [[Chapter 7]]. | Sometimes people call [[Lookup]] ''magical bag''. The pejorative flavor of the nickname is based on observation that you can put your hand in it and pray to find the object you are seeking for. Sometimes people believe there shall be a better way. Yes, in many cases there is. [[Lookup]] is not handy for everything. It is basically a tool to implement a [[teleinterface]]s. In case a [[teleinterface]] is not what you want (like in case of [[CumulativeFactory]]), you'd rather prefer type safety and design proper [[API]] without ''magical bag'' - aka [[Lookup]]. More about that in [[Chapter 7]]. |