logs of my work and digital life
Posts tagged development
Design Patterns: Iterator
Jul 8th
Hi Guys,
I am reading more on design patterns from thisĀ article after little theory session here is working code. Key point was all of the collection classes in the System.Collections namespace, as well as arrays, implement IEnumerable and can therefore be iterated over.
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class IteratorPattern
{
static void Main(string[] args)
{
int[] values = new int[] { 1, 2, 3, 4, 5 };
IEnumerator<int> e = ((IEnumerable<int>)values).GetEnumerator();
while (e.MoveNext())
{
Console.Write(e.Current.ToString() + " ");
}
Console.ReadKey();
}
}
}
Observer pattern with .net
Jul 8th
Hi folks,
Well I am investigating design patterns by reading an article so after reading a bit I thought it would be nice if I write some code and test it.
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
/// <summary>
/// Product Class
/// </summary>
public class Product
{
public delegate void NameChangeEventHandler(int a);
public event NameChangeEventHandler NameChanged;
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
if (NameChanged != null)
{
NameChanged(5);
}
}
}
public Product(string name)
{
Name = name;
}
public void PrintName()
{
Console.WriteLine("ProductName={0}", Name);
}
}
/// <summary>
/// Test Class
/// </summary>
public class Test
{
public Test(Product P)
{
P.NameChanged += new Product.NameChangeEventHandler(ChangeDetected);
}
public void ChangeDetected(int a)
{
Console.WriteLine("change found,argument passed is {0}",a);
}
}
static void Main(string[] args)
{
Product p = new Product("najam awan");
p.PrintName();
Test t = new Test(p);
p.Name = "najaf awan";
p.PrintName();
Console.ReadKey();
}
}
}
Custom Event for Classes
Jul 6th
I was reading how to write events for your custom classes so after reading a little I thought give it a shot and created this sample code. Below code is using Product class with event and delegate for event handling and other class named Test is using this class and manipulate product class data. If you change the Name of the product event will fire.
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
/// <summary>
/// Product Class
/// </summary>
public class Product
{
public delegate void NameChangeEventHandler(int a);
public event NameChangeEventHandler NameChanged;
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
if (NameChanged != null)
{
NameChanged(5);
}
}
}
public Product(string name)
{
Name = name;
}
public void PrintName()
{
Console.WriteLine("ProductName={0}", Name);
}
}
/// <summary>
/// Test Class
/// </summary>
public class Test
{
public void ChangeDetected(int a)
{
Console.WriteLine("change found,argument passed is {0}",a);
}
public void TestProduct()
{
Product p = new Product("najam awan");
p.PrintName();
p.NameChanged += ChangeDetected;
p.Name = "najaf";
p.PrintName();
Console.ReadKey();
}
}
static void Main(string[] args)
{
Test t = new Test();
t.TestProduct();
}
}
}