Skip to content

Commit

Permalink
Iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasCancio committed Jan 14, 2021
1 parent 97819a6 commit cdfeedf
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 0 deletions.
9 changes: 9 additions & 0 deletions PatternsComportamentais/Iterator/Aggregate.cs
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();
}
}
26 changes: 26 additions & 0 deletions PatternsComportamentais/Iterator/ConcretAggregate.cs
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); }
}
}
}
39 changes: 39 additions & 0 deletions PatternsComportamentais/Iterator/ConcretIterator.cs
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;
}
}
}
12 changes: 12 additions & 0 deletions PatternsComportamentais/Iterator/Iterator.cs
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();
}
}
8 changes: 8 additions & 0 deletions PatternsComportamentais/Iterator/Iterator.csproj
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>
29 changes: 29 additions & 0 deletions PatternsComportamentais/Iterator/Program.cs
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();
}
}
}
}
6 changes: 6 additions & 0 deletions PatternsComportamentais/PatternsComportamentais.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Command", "Command\Command.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Interpreter", "Interpreter\Interpreter.csproj", "{3854DE72-2962-43F0-8E6C-3112F6AEE85A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Iterator", "Iterator\Iterator.csproj", "{DDA2348A-5257-4E65-9400-2C95DE6F6514}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +29,10 @@ Global
{3854DE72-2962-43F0-8E6C-3112F6AEE85A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3854DE72-2962-43F0-8E6C-3112F6AEE85A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3854DE72-2962-43F0-8E6C-3112F6AEE85A}.Release|Any CPU.Build.0 = Release|Any CPU
{DDA2348A-5257-4E65-9400-2C95DE6F6514}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DDA2348A-5257-4E65-9400-2C95DE6F6514}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DDA2348A-5257-4E65-9400-2C95DE6F6514}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DDA2348A-5257-4E65-9400-2C95DE6F6514}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit cdfeedf

Please sign in to comment.