Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Thursday, September 11, 2014

Observer pattern

To describe the Observer pattern, lets take an example of Football game.
The specific design problem is,

When the position of a ball changes, all the players should be notified straight away. 


Define a one-to-many dependency between objects so that one object changes state, all its dependents are notified and updated automatically.




Subject (Interface) exposes us three methods Attach, detach and notify. 
ConcreteSubject is football. Which exposes the current position to observers.
Observer (interface) exposes the update method.
ConcreteObserver is player. Which implements update method and with in this method it calls for the position of the ball and set the observerstate to the same.

ref: applying design pattern

Wednesday, February 19, 2014

Difference between Events and Delegates

Events are basically actions raised by an object when some condition met.
Like In stock class there is a property named Limit, and it raised an notification when it reaches certain limit. This notification is done by events


Delegate is more generic term which is similar to a pointer to a function.
They are generally used as a input parameter. In particular they are perfect way to implement Strategy patternFor example, if I want to sort a List of objects, I can provide a Compare strategy to the method to tell the implementation how to compare two objects.


Saturday, June 11, 2011

Multi level xml generation using XMLSerialization in C#.Net


//In memory XML generation using Serialization//
XmlSerializer srl = new XmlSerializer(typeof(UserLogin));
StringWriter sw = new StringWriter();
srl.Serialize(sw, oUserLogin);
string xml = sw.GetStringBuilder().ToString();


[Serializable]
[XmlRoot("Users")]
public class UserDetails
{
//First way to create inner XML nodes
[XmlArray("AddressDetails")]
public List AddressDetail { get; set; }

//Second way to create inner XML nodes
[XmlArray("ContactDetails")]
[XmlArrayItem("ContactDetail")]
public List ContactDetail { get; set; }
}


[Serializable]
[XmlType("AddressesDetail")]
public class AddressDetails
{
public string ........ (Properties)
}

[Serializable]
public class ContactDetails
{
public string ........ (Properties)
}