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
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.▼
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>:▼
▲
<source lang="Java">
public class Generator extends AbstractList<Integer> {
Line 255 ⟶ 258:
</source>
<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 ===
|