Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Consider having multiple object pools depending on what you want to get back. What if you had the package structure of (and forgive me for the naming):</p> <pre><code>com.foo.bar.AbstractTeacher com.foo.bar.Teacher // A factory com.foo.bar.BioTeacher com.foo.bar.PhysicsTeacher ... </code></pre> <p>Each of these classes maintains its own object pool. BioTeacher contains just BioTeacher, while Teacher contains all the Teacher objects</p> <p>Thus, you would then have code such as:</p> <pre><code>Teacher t = Teacher.getTeacher(); BioTeacher b = BioTeacher.getTeacher(); </code></pre> <p>This would simplify the casting from the more generic pool as you can specify what type of teacher you want before getting one (though yes, it does make the synchronization between the Teacher class and the BioTeacher class pools more complex).</p> <p>The way this would be approached by making package level methods (they can be called by other classes in the same package (notice how all the classes are in <code>com.foo.bar</code>), and unable to be called by objects outside of that package.</p> <p>Filling the pool would be done with either</p> <pre><code>Teacher.createTeacher(BioTeacher.class) </code></pre> <p>Which would then register the teacher in the BioTeacher pool or:</p> <pre><code>BioTeacher.createTeacher() </code></pre> <p>Which would then register the teacher in the Teacher pool.</p> <p>The tricky part if working in a multithreaded environment (something to keep in mind as this is a learning experience is in the methods <code>Teacher.getTeacher()</code> and <code>BioTeacher.getTeacher()</code> where one would possibly have a race condition where one thread gets the Teacher from getTeacher while another thread gets one from the BioTeacher.</p> <hr> <p>Another approach to this (forget about the stuff of multiple constructors from above) is to maintain a map of <code>Class,List&lt;Teacher&gt;</code> within the <code>Teacher</code> class itself. When you ask for a teacher (<code>Teacher.getTeacher(Teacher.class)</code> or <code>Teacher.getTeacher(BioTeacher.class)</code>) you look it up in the map and return an item from the appropriate list. Furthermore, in this method (that can be synchronized) you can then find the teacher in the other list (it will always be in at least two lists (Teacher.class and BioTeacher.class)) and remove it from that list too.</p> <p>Then you can make the code so that:</p> <pre><code>BioTeacher Teacher.getTeacher(BioTeacher.class) </code></pre> <p>works without additional castings. Again, this is a learning experience and will get you into learning more about Java's generics. (More about this approach at <a href="https://stackoverflow.com/questions/9790908/java-6-how-return-instance-of-object-of-same-type-as-class-passed-in">How do I return an instance of an object of the same type as the class passed in using Java 6?</a> )</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    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