Introduction
In this article, I am going to discuss about Loosely Coupled collection. Due to addition of Generic collections in C#, it became quite common to declare collection along with the type of objects; it can store in it. But, sometimes we might need a custom collection that will allow storing any kind of object's collection in it. I will explain this collection pattern followed by an example. Iterators provide an abstract mechanism to access elements in a collection without revealing its underlying implementation. If we use Iterators efficiently, it will create a loose coupling between the collection and its client.
I mean, we can leave the option of selecting the type of data structure for storing elements in client's hand.
Scenarios applicable for using this Collection:
- If we want to store a group of items belonging to different collection types like Hash table, Hash set etc.
- If the type of collection used to store elements changes frequently because of client's request or introduction of better collections like generic List, Hash set etc.
Benefits of using this Pattern:
- We can add elements of any collection type.
- More flexibility, since client will be having the choice of selecting appropriate collection type for storing the elements.
Implementation:
Create a new console application and name it as LooseCouplingIteratorSample. Now, add the code to program.cs as shown below:
class Program
{
static void Main(string[] args)
{
//Client Code...
//Tight coupled Collection
TightCoupledCollection col1 = new TightCoupledCollection();
//Here, we can add only Hashtable...
Hashtable hTable1 = new Hashtable();
hTable1.Add(30, 40);
col1.AddList(hTable1);
//Loose coupled Collection
//Here, we can add collections of any type that are implementing IEnumerable...
LooseCoupledCollection col = new LooseCoupledCollection();
Hashtable hTable = new Hashtable();
HashSet<int> list = new HashSet<int>();
List<string> slist = new List<string>();
hTable.Add(1, 100);
hTable.Add(12, 100);
list.Add(20);
list.Add(30);
slist.Add("test");
slist.Add("abc");
col.AddList(hTable);
col.AddList(list);
col.AddList(slist);
Console.ReadLine();
}
}
class LooseCoupledCollection
{
private System.Collections.ArrayList list = new ArrayList();
public void AddList(IEnumerable e)
{
IEnumerator item = e.GetEnumerator();
while (item.MoveNext())
{
list.Add(item.Current);
}
}
}
class TightCoupledCollection
{
private System.Collections.ArrayList list = new ArrayList();
public void AddList(Hashtable hTable)
{
IEnumerator item = hTable.GetEnumerator();
while (item.MoveNext())
{
list.Add(item.Current);
}
}
}
In tightly coupled collection, we are specifying the exact type of collection; it will accept. Because of this, the collection is going to depend on a specific built-in collection like hash table. But in loosely coupled one, it can accept any collection as long as it implements IEnumerable.
I am ending up the things here. I am attaching source code for reference. I hope this article will be helpful for all.