Sunday, August 27, 2017

Iterator Pattern


Interpreter design pattern is used to access elements of collection sequentially. Iterator Pattern falls under behavioral pattern of GOF (Gang of Four) patterns.

When to use –


Iterator pattern provides a way to access elements of collection sequentially without exposing its representation. The client code should not be concerned about the actual implementation of traversing the elements of collection. The iterator pattern encapsulates such implementation and provides common way to traverse different types of collection.

Major components of Iterator pattern –


Client – This class creates an instance of collection object (ConcreteAggreate) and iterator object to perform next operation on collection to retrieve next items until all items are retrieved.

Iterator – The Iterator interface defines all the methods to traverse the collection.

ConcreteIterator – This class implements the Iterator interface and provides actual implementation of how the elements are traverse in collection.

Aggregate – This is an abstract class. This class defines method to create an Iterator.

ConcreteAggregate – This class will be derived from abstract class Aggregate and implements and creates an iterator object.

Let’s have a look on below example of Iterator pattern.

Code –

class Program
{
    //Client
    static void Main(string[] args)
    {
        //Create myObject collection and items to it.
        MyObjectCollection aggr = new MyObjectCollection();
        aggr.Add(10);
        aggr.Add(-20);
        aggr.Add("Test");
        aggr.Add(33);
        aggr.Add("Iterator");
        aggr.Add(52.6);

        //Get Iterator and iterator all items
        Iterator itor = aggr.GetIterator();
        while (!itor.IsDone())
        {
            Console.WriteLine(itor.CurrentItem().ToString());
            itor.Next();
        }
        Console.ReadLine();
    }
}
//Iterator
public interface Iterator
{
    object First();
    object Next();
    object CurrentItem();
    bool IsDone();
}
//ConcreteIterator
public class MyIterator : Iterator
{
    private MyObjectCollection aggregate;
    int index;
    public MyIterator(MyObjectCollection aggr)
    {
        this.aggregate = aggr;
        index = 0;
    }
    public object CurrentItem()
    {
        return aggregate[index];
    }
    public object First()
    {
        return aggregate[0];
    }
    public bool IsDone()
    {
        return index >= aggregate.Count ;
    }
    public object Next()
    {
        object retVal = null;
        if (index < aggregate.Count)
            retVal = aggregate[index++];
        return retVal;
    }
}
//Aggregate
public abstract class Aggregate
{
    public abstract Iterator GetIterator();
}
//ConcreteAggregate
public class MyObjectCollection : Aggregate
{
    private ArrayList myList = new ArrayList();

    public override Iterator GetIterator()
    {
        return new MyIterator(this);
    }
    public object this[int index]
    {
        get { return myList[index]; }
    }
    public int Count
    {
        get { return myList.Count; }
    }
    public void Add (object obj)
    {
        myList.Add(obj);
    }
}

Output –


As you can see in this example, I created an instance of MyObjectCollection class and added few data items to it. I used ArrayList to add any object to the collection. You can use any type as per your requirement. You can also use generic type here. I get instance of iterator from MyObjectCollection instance and I’m iterating all the items of my collection using iterator instance. I’m using Iterator.Next() method to iterate to next item and Iterator.CurrentItem() to get current item of collection.

The Iterator patterns are available inbuilt with Microsoft .Net.  It provides two interfaces IEnumerable and IEnumerator. You can make your Iterator collection implementing IEnumerable interface. Almost all majorly used collections available in .Net implements IEnumerable interface. The few examples of inbuilt collections are List<T>, Dictionary<T>, ArrayList, SortedList etc. See below definition of both the interfaces.

public interface IEnumerable
{
        IEnumerator GetEnumerator();
}

public interface IEnumerator
{
        object Current { get; }
        bool MoveNext();
        void Reset();
}

Let’s have a look on below example of custom collection implementing IEnumerable interface.

class Program
{
    static void Main(string[] args)
    {
        MyCollection coll = new MyCollection();
        foreach(var item in coll)
            Console.WriteLine(item.ToString());

        Console.ReadLine();
    }
}

public class MyCollection : IEnumerable
{
    List<int> myList = new List<int>() { 2, 5, 3, 6, 4, 9 };
    public IEnumerator GetEnumerator()
    {
        foreach (var item in myList)
            yield return item;
    }
}

Output –



As per above code, IEnumerable provides GetEnumerator method which will return IEnumerator list. This list iterated through foreach loop in main class. The yield return keyword can be used to provide a value to the enumerator object.

You can download full code of Iterator Design Pattern from here. You can download code for Iterator Pattern implementing IEnumerable interface from here

I hope this article helps you to know more about Iterator Design Pattern. Please leave your feedback in comments section below.

References –

See Also –

Sunday, July 23, 2017

Interpreter Pattern


Interpreter design pattern majorly used with languages and notations. This pattern can be useful to convert from one language to another. This pattern allows to write multiple rules or grammar for language to interpret and extend easily. Interpreter Pattern falls under behavioral pattern of GOF (Gang of Four) pattern.

When to use –


Interpreter Pattern is useful to interpret languages where rules/grammar are complex part which you can easily extend using this pattern. The pattern works on hierarchy of expression. The expression can be classified as Terminal or Non-Terminal. The terminal expression is standalone and can be Interpreted immediately. The non-terminal expression may contain one or more terminal or non-terminal expressions.  

Major components of Interpreter pattern –


Client – This client class responsible for build context, terminal and non-terminal expression objects. It also responsible to make call to interpret method of respective expression.
Context – This is context class used to store input/output data and other information required by expression.
Expression – This is an interface which defines Interpret method which will be implemented by sub classes.
TerminalExpression – This class will implement Expression interface and implements Interpret method.
NonTerminalExpression – This class will implement Expression interface and implements Interpret method similar to TerminalExpression class. This class may contain one or more terminal or non-terminal expressions and invoke Interpret method respectively.

There are many examples available for Interpreter design pattern. I would like to take simple example to understand the intent and implementation for Interpreter design pattern. In my example I would like interpret numbers to words. So I’ll provide number as input and that will be interpreted by pattern to words. Similarly, you can add multiple rules to interpret as per your requirement.

See below example of Interpreter pattern.

Code –
//Client class
class Program
{
    static void Main(string[] args)
    {
        Context ctx = new Context(5642);
        IExpression exp1 = new WordExpression();
        exp1.Interpret(ctx);

        Context ctx1 = new Context(397);
        SubExpression subExp = new SubExpression();
        subExp.Expression1 = new WordExpression();
        subExp.Interpret(ctx1);

        Console.ReadLine();
    }
}
//Context class
public class Context
{
    public int Input { get; set; }
    public Context(int inputValue)
    {
        Input = inputValue;
    }
}
//Expression Interface
public interface IExpression
{
    void Interpret(Context context);
}
//Terminal expression class
public class WordExpression : IExpression
{
    public Dictionary<int, string> numberWord = new Dictionary<int, string>() {
        { 1, "One" }, { 2, "Two" }, { 3, "Three" }, { 4, "Four" }, { 5, "Five" },
        { 6, "Six" }, { 7, "Seven" }, { 8, "Eight" }, { 9, "Nine" }, { 0, "Zero" } };
    public void Interpret(Context context)
    {
        Console.Write("Input Value {0} - ", context.Input);
        string value = context.Input.ToString();
        if (value.Length > 0)
        {
            foreach (char chr in value)
            {
                int number = (int)Char.GetNumericValue(chr);
                Console.Write("{0} ", numberWord[number]);
            }
            Console.WriteLine();
        }
    }
}
//Non-terminal expression class
public class SubExpression : IExpression
{
    public IExpression Expression1 { get; set; }
    public void Interpret(Context context)
    {
        Console.WriteLine("Running Sub Expression 1");
        Expression1.Interpret(context);
    }
}

Output –


As you can see in this example, user set context value to 5642 and Interpret it to words via calling WordExpression which is terminal expression. User also set value another context object ctx1 and set 397 value to Interpret to word via calling SubExpression which is non-terminal expression and contains reference of terminal expression. You can add multiple conversion rules and interpret methods via adding multiple expressions as per your requirement.

You can download full code from Gist.

I hope this article helps you to know more about Interpreter Design Pattern. Please leave your feedback in comments section below.

References –

See Also –