"The Crow" <q> a écrit dans le message de (e-mail address removed)...
|
http://www.dofactory.com/Patterns/Patterns.aspx
But bear in mind that some of the examples are by no means optimal due to
language features available in C#.
e.g. The Observer pattern using a multicast delegate :
public interface ISubject
{
event EventHandler Notify;
}
public interface IObserver
{
void Update(object sender, EventArgs e);
}
public class MySubject : ISubject
{
private EventHandler notify;
private void OnNotify(EventArgs e)
{
if (notify != null)
notify(this, e);
}
event EventHandler ISubject.Notify
{
add { notify += value; }
remove { notify -= value; }
}
}
public class MyObserver : IObserver
{
void IObserver.Update(object sender, EventArgs e)
{
Console.WriteLine(sender.ToString());
}
}
You don't have to use interfaces, but they provide the ability to derive
from other classes and to implement a pattern as well.
Test code can look something like this :
{
MySubject subject = new MySubject();
MyObserver observer = new MyObserver();
((ISubject) subject).Notify += ((IObserver) observer).Update;
subject.Trigger();
Console.ReadLine();
}
Joanna