Skip to content

Commit

Permalink
Add Iterator pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
peterm85 committed Nov 22, 2019
1 parent 1c573a2 commit 5ee7423
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Forma en que las clases u objetos interaccionan y distribuyen funcionalidades.
* [Memento](#memento) [**O**]
* [Mediator](#mediator) [**O**]
* [Template method](#template) [**C**]
* [Iterator] [**O**]
* [Iterator](#iterator) [**O**]
* [Visitor] [**O**]
* [State] [**O**]
* [Interpreter] [**C**]
Expand Down Expand Up @@ -403,3 +403,19 @@ Permite redefinir ciertos pasos seguros de un algoritmo sin cambiar la estructur
* [Order Managing](https://github.com/peterm85/design-patterns/tree/master/src/template/examples/ordermanaging)

**Referencia:** [https://www.geeksforgeeks.org/template-method-design-pattern/](https://www.geeksforgeeks.org/template-method-design-pattern/)

## <a name="iterator">Iterator</a> [&#8593;](#lista-de-patrones)

**Propósito:** Define una interfaz que declara los métodos necesarios para acceder secuencialmente a un grupo de objetos de una colección.

Algunos de los métodos que podemos definir en la interfaz Iterador son:

Primero(), Siguiente(), HayMas() y ElementoActual().

**Aplicación:** Usamos el patrón Iterator cuando ...
* Se desea acceder a los elementos de un contenedor de objetos (por ejemplo, una lista) sin exponer su representación interna.

**Ejemplos:**
* [Vectors](https://github.com/peterm85/design-patterns/tree/master/src/iterator/examples/vectors)

**Referencia:** [https://www.geeksforgeeks.org/template-method-design-pattern/](https://www.geeksforgeeks.org/template-method-design-pattern/)
14 changes: 14 additions & 0 deletions src/iterator/examples/vectors/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package iterator.examples.vectors;

public class Client {
public static void main(String argv[]) {
Vector vector = new Vector(5);

//Creación del iterador
VectorIterator iterador = vector.iterador();

//Recorrido con el iterador
while (iterador.hasNext())
System.out.println(iterador.next());
}
}
28 changes: 28 additions & 0 deletions src/iterator/examples/vectors/Vector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package iterator.examples.vectors;

public class Vector {
public int[] _datos;

public Vector(int valores){
_datos = new int[valores];
for (int i = 0; i < _datos.length; i++){
_datos[i] = i;
}
}

public int getValor(int pos){
return _datos[pos];
}

public void setValor(int pos, int valor){
_datos[pos] = valor;
}

public int dimension(){
return _datos.length;
}

public VectorIterator iterador(){
return new VectorIterator(this);
}
}
24 changes: 24 additions & 0 deletions src/iterator/examples/vectors/VectorIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package iterator.examples.vectors;

public class VectorIterator {
private int[] _vector;
private int _posicion;

public VectorIterator(Vector vector) {
_vector = vector._datos;
_posicion = 0;
}

public boolean hasNext(){
if (_posicion < _vector.length)
return true;
else
return false;
}

public Object next(){
int valor = _vector[_posicion];
_posicion++;
return valor;
}
}

0 comments on commit 5ee7423

Please sign in to comment.