Generator (computer programming): Difference between revisions

Content deleted Content added
Java: removing spam; adding the standard way of doing that in Java
Java: clearer
Line 225:
 
=== Java ===
[[Java (programming language)|Java]] does not have [[continuation]] functionality as part of the language ''per se''; however, one may easily obtain continuationsthat asby objectsproviding a custom implementation of some known collection or iterator interface. This will whichtypically implementbe the <tt>java.lang.Iterable</tt> interface., The object will act asor a factory[[Java ofcollections numbers,framework]] creating them on the flyinterface, whilebut theit applicationmight willeven onlybe "see"a andcustom usecollection the iterator methods of that objectinterface.<br/>
The continuation is implemented through a finite-state machine, namely an object, that will compute the single elements on the fly, instead of retrieving them from a real collection or storage. This will be hidden behind the collection interface, and therefore will be transparent to the application, which only "sees" the iterator or collection methods of that object, and does not know about its implementation internals, thanks to [[Encapsulation (object-oriented programming)|encapsulation]].<br/>
This means that, although continuations are not available as part of the language, you can easily get them through an OOP solution, specifically through [[Polymorphism_in_object-oriented_programming|polymorphism]].
 
It is strongly advised that this technique be used whenever it is applicable. For example, it is often applied when implementing some ''very simple'' implementations of models for a list or table in a [[Swing (Java)|Swing]] application: the model is implemented so that is generates the contents of the model on the fly, and this is completely transparent to the Swing library, because it just sees a collection of elements.
There are essentially two ways of doing this:
#implement Iterable indirectly, e.g. through some abstract class which is part of the Java Collection Framework;
#implements Iterable directly.
 
It is also important to notice that, as long as the generator object implements interface <tt>java.lang.Iterable</tt>, it may be used to control a ''for each'' statement.
In the first case, you will typically extend the J2SE class AbstractList to implement an unmodifiable list<ref>[http://download.oracle.com/javase/6/docs/api/java/util/AbstractList.html java.util.AbstractList]</ref>:
 
 
InLet's thetake firstan case,example. youThe willfollowing typicallycode extenddefines a subclass of the the J2SE class AbstractList, to implement an unmodifiable list<ref>[http://download.oracle.com/javase/6/docs/api/java/util/AbstractList.html java.util.AbstractList]</ref>:
<source lang="Java">
public class Generator extends AbstractList<Integer> {
Line 255 ⟶ 258:
</source>
 
SuchAll of the collections in the [[Java collections framework]] implement <tt>java.lang.Iterable</tt>, therefore such a list can then be used in a ''for each'' statement, for example:
<source lang="java">
for(int value: new Generator(7, 20))
Line 264 ⟶ 267:
 
 
There also exist some third-party libraries, which allow for implementing generators by subclassing a given abstract class.
 
 
It is strongly advised that this technique be used whenever it is applicable. For example, it is often applied when implementing some very simple implementations of models for a list or table in a [[Swing (Java)|Swing]] application: the model is implemented so that is generates the contents of the model on the fly, and this is completely transparent to the Swing library, because it just sees a collection of elements.
 
=== Perl ===