Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are several options to enforce this.</p> <ul> <li>At build time you could write <strong>tests for each</strong> of the <code>Fruit</code> classes that look for a field that satisfies your requirements.</li> <li>At build time you could write a <strong>single test</strong> that goes through your entire classpath and verifies that each <code>Fruit</code> classes satisfies your requirements. A library like <a href="https://code.google.com/p/reflections/" rel="nofollow">Reflections</a> could help you to achieve this.</li> <li>At compile time you could <a href="http://www.javalobby.org/java/forums/t17876.html" rel="nofollow"><strong>process an Annotation</strong></a>. I am not sure how you would make sure that each of your classes had an Annotation (as opposed that each class that contains an Annotation is one of the classes in your set.)</li> <li>At implementation time, as a slight variation on your request, you could <strong>use an abstract class</strong> instead of an interface and require all implementors to hand you the fixed data in the constructor. That way, you have absolute control over the behaviour.</li> <li>At runtime, <strong>while the application launches</strong>, you could check that all implementing classes satify your requirements in the same way an integration test would do it. In a scenario where third parties contribute to your API, this might be the last-stop option if you absolutely have to check it.</li> </ul> <p>I think it is best to use tests for this. You'll have all the certainty you need with far better feedback and much less effort. If tests are not an option, because you can't control the implementers, I'd go for the abstract class with enforcement during launch as a last resort.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload