←Older revision | Revision as of 06:07, 15 July 2008 | ||
Line 13: | Line 13: | ||
<source lang="java"> | <source lang="java"> | ||
- | + | package javax.whatnot; | |
- | + | public final class Stuff { | |
- | + | private StuffProvider impl; | |
- | + | private Stuff(StuffProvider impl) {...} | |
- | + | public static Stuff forPlace(Place place) { | |
- | + | return new Stuff(findImplForPlace(place)); | |
- | + | } | |
- | + | public String name() { | |
- | + | return impl.name(); | |
- | + | } | |
- | + | } | |
- | + | package javax.whatnot.spi; | |
- | + | public interface StuffProvider { | |
- | + | String name(); | |
- | + | } | |
</source> | </source> | ||
Line 33: | Line 33: | ||
<source lang="java"> | <source lang="java"> | ||
- | + | package javax.whatnot; | |
- | + | public final class Stuff { | |
- | + | private StuffProvider impl; | |
- | + | private Stuff(StuffProvider impl) {...} | |
- | + | public static Stuff forPlace(Place place) { | |
- | + | return new Stuff(findImplForPlace(place)); | |
- | + | } | |
- | + | public String name() { | |
- | + | return impl.name(); | |
- | + | } | |
- | + | public long distance() { | |
- | + | return impl instanceof PossiblyDistantStuffProvider ? | |
- | + | ((PossiblyDistantStuffProvider) impl).distance() : | |
- | + | /* fallback value */ 0L; | |
- | + | } | |
- | + | } | |
- | + | package javax.whatnot.spi; | |
- | + | public interface StuffProvider { | |
- | + | String name(); | |
- | + | } | |
- | + | public interface PossiblyDistantStuffProvider extends StuffProvider { | |
- | + | String name(); | |
- | + | long distance(); | |
- | + | } | |
</source> | </source> | ||
Line 62: | Line 62: | ||
<source lang="java"> | <source lang="java"> | ||
- | + | package javax.whatnot; | |
- | + | public abstract class Stuff { | |
- | + | public Stuff forPlace(Place place) {...} | |
- | + | protected Stuff() {} | |
- | + | public abstract String name(); | |
- | + | public long distance() {/*fallback*/ return 0L;} | |
- | + | } | |
</source> | </source> | ||
Line 76: | Line 76: | ||
<source lang="java"> | <source lang="java"> | ||
- | + | package api; | |
- | + | public Icon getIcon() { | |
- | + | return implV1 != null ? implV1.getImageIcon() : implV2.getIcon(); | |
- | + | } | |
- | + | public ImageIcon getImageIcon() { | |
- | + | return implV1 != null ? implV1.getImageIcon() : | |
- | + | new ImageIcon(icon2Image(implV2.getIcon())); | |
- | + | } | |
</source> | </source> | ||
Line 89: | Line 89: | ||
<source lang="java"> | <source lang="java"> | ||
- | + | public interface TotallyDifferentStuffProvider { | |
- | + | long xCoord(); | |
- | + | long yCoord(); | |
- | + | } | |
</source> | </source> | ||
Line 98: | Line 98: | ||
<source lang="java"> | <source lang="java"> | ||
- | + | name() -> googleMapsWebServices.locate(impl.xCoord(), impl.yCoord()). | |
- | + | nameOfMunicipality() | |
- | + | distance() -> sqrt(impl.xCoord()**2 + impl.yCoord()**2) | |
</source> | </source> | ||