You have to create two interfaces and their derived concrete classes. Your service will create IObservable, IObserver and ApplicationErrorState.
你的客户将创建班级,并从我观察员那里获得ErrorMessageSync。
Here is my example which displays errors and is created with .Net 3.5.
www.un.org/spanish/ecosoc P.S. 也可使用代表<>。
/// It will store observers and will push the message
public interface IErrorObservable
{
void Attach(IErrorObserver observer);
void Detach(IErrorObserver observer);
void Notify();
}
public interface IErrorObserver
{
void Update(string message);
}
///It is concrete class to push message
public sealed class ApplicationErrorState : IErrorObservable
{
private List<IErrorObserver> _observers = new List<IErrorObserver>();
///constructor
public ApplicationErrorState()
{
}
public void Attach(IErrorObserver observer)
{
_observers.Add(observer);
}
public void Detach(IErrorObserver observer)
{
_observers.Remove(observer);
}
public void Notify()
{
foreach (IErrorObserver observer in _observers)
{
observer.Update(/*Logic*/);
}
}
public void SetError()
{
Notify();
}
///COncrete subject
private class ErrorMessageSync : IErrorObserver
{
private MyClass _parent;
public ErrorMessageSync(MyClass parent)
{
_parent = parent;
}
public void Update(string message)
{
//This work will be done
}
}
这种方法就是UML。