forked from LucasCancio/design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97819a6
commit cdfeedf
Showing
7 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System; | ||
|
||
namespace Iterator | ||
{ | ||
public abstract class Aggregate | ||
{ | ||
public abstract Iterator CreateIterator(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Collections; | ||
|
||
namespace Iterator | ||
{ | ||
public class ConcretAggregate : Aggregate | ||
{ | ||
|
||
private ArrayList _items = new ArrayList(); | ||
public override Iterator CreateIterator() | ||
{ | ||
return new ConcretIterator(this); | ||
} | ||
|
||
public int Count | ||
{ | ||
get { return _items.Count; } | ||
} | ||
|
||
public object this[int index] | ||
{ | ||
get { return _items[index]; } | ||
set { _items.Insert(index, value); } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
|
||
namespace Iterator | ||
{ | ||
public class ConcretIterator : Iterator | ||
{ | ||
private ConcretAggregate _aggregate; | ||
private int _current = 0; | ||
|
||
public ConcretIterator(ConcretAggregate aggregate) | ||
{ | ||
this._aggregate = aggregate; | ||
} | ||
|
||
public override object CurrentItem() | ||
{ | ||
return _aggregate[_current]; | ||
} | ||
|
||
public override object First() | ||
{ | ||
return _aggregate[0]; | ||
} | ||
|
||
public override bool IsDone() | ||
{ | ||
return _current >= _aggregate.Count; | ||
} | ||
|
||
public override object Next() | ||
{ | ||
object ret = null; | ||
if (_current < _aggregate.Count - 1) | ||
ret = _aggregate[++_current]; | ||
|
||
return ret; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
|
||
namespace Iterator | ||
{ | ||
public abstract class Iterator | ||
{ | ||
public abstract object First(); | ||
public abstract object Next(); | ||
public abstract bool IsDone(); | ||
public abstract object CurrentItem(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
|
||
namespace Iterator | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
ConcretAggregate a = new ConcretAggregate(); | ||
|
||
a[0] = "Item A"; | ||
a[1] = "Item B"; | ||
a[2] = "Item C"; | ||
a[3] = "Item D"; | ||
|
||
Iterator i = a.CreateIterator(); | ||
|
||
Console.WriteLine("Iteragindo com a coleção: "); | ||
|
||
object item = i.First(); | ||
|
||
while (item!=null) | ||
{ | ||
Console.WriteLine(item); | ||
item = i.Next(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters