Iterator,Pattern

Erhan 8/7/2016 0

Iterator Pattern

C#
 Challenge
You are working on a Name printing application. You have to deal with a Bank class
with the following properties.
public class Bank
{
public string ManagerName
{
get;
set;
}
public string AccountantName
{
get;
set;
}
public string CashierName
{
get;
set;
}
}
For printing the name the Print() method is called as following:
Print(_bank.Manager);
Print(_bank.Accountant);
Print(_bank.Cashier);
Now there is an addition of Branch type Bank to the above Bank object. Now the
situation has become more complex. How to do a better approach?

Definition
GoF Definition: "Provide a way to access the elements of an aggregate object
sequentially without exposing its underlying representation"
Implementation
We can improve the above Challenge Situation using the Iterator pattern. Instead of
calling Print() method each time, we can use an Iterator to get the names inside Bank
Object.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IteratorPattern
{
public class Bank
{
    public string Manager
    {
        get;
        set;
    }

    public string Accountant
    {
        get;
        set;
    }

    public string Cashier
    {
        get;
        set;
    }
}   
}

*****************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace IteratorPattern
{
    public class BankIterator : IEnumerable
    {
        private IList _list = new ArrayList();

        public BankIterator(Bank bank)
        {
            _list.Add(bank.Manager);
            _list.Add(bank.Accountant);
            _list.Add(bank.Cashier);
        }

        public IEnumerator GetEnumerator()
        {
            return _list.GetEnumerator();
        }
    }
}

***************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace IteratorPattern
{
    public partial class MainForm : Form
    {
        private Bank _bank = new Bank();

        public MainForm()
        {
            InitializeComponent();
            _bank.Manager = "Manager";
            _bank.Accountant = "Accountant";
            _bank.Cashier = "Cashier";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Print(_bank.Manager);
            Print(_bank.Accountant);
            Print(_bank.Cashier);
        }

        private void Print(string name)
        {
            // Print the name
            MessageBox.Show(name   " printed.." );
        }

        private void button2_Click(object sender, EventArgs e)
        {
            BankIterator iterator = new BankIterator(_bank);
            foreach (string name in iterator)
                Print(name);
        }
    }
}

  
 

Report Bug

Please Login to Report Bug

Reported Bugs

Comments

Please Login to Comment

Comments