The Observer pattern is now not a lot of focus. Look at its design ideas OK, let's continue with the message center.
<summary>
///Data center
///</summary> Public
class MessageData
{public
void Messagesend ()
{
string title = Observer.gettitle ();
String content = Observer.getcontent ();
Programming for specific implementations can cause us to platformlist or even refactor code
foreach (Platform p in observer) when we add or remove message platforms later. Platformlist)
{
p.update (title, content);
}
}
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/
The above scenario is more serious in the SOA architecture, and basically the data center as a base station program is not suitable for the version to update frequently
So how does this relate to a one-to-many relationship?
With the observer pattern, the data center is the object of the specific state and can control these states. In other words, there is a "one" with a state of the data body. On the other hand, the observer pattern uses these states, although these states do not belong to them. There are many observers who rely on themes to tell them that the state is appropriate to change. This creates a relationship between the "one" datacenter and the "multiple" message publishers.
The power of loose coupling: When two objects are loosely coupled, they can still interact, but do not know each other's details. The Observer pattern provides an object design that allows for loose coupling between the subject and the observer.
<summary>///Data Center///</summary> public class Messagedata:messagedateif {Priv
Ate ArrayList observers;
private string title;
private string content;
Public MessageData () {observers = new ArrayList (); public void Registerplatform (Observerif o) {observers.
ADD (o); The public void Removeplatform (Observerif o) {int i = observers.
IndexOf (o); if (i > 0) {observers. Remove (i);
} public void Notifyplatform () {foreach (Observerif o in observers)
{o.update (title, content); } public void Messagechanged (string title,string content) {This.title
= title;
this.content = content;
Notifyplatform (); }} public interface Observerif {
void Update (string title, string content);
public interface Messagedateif {void Registerplatform (Observerif o);
void Removeplatform (Observerif o);
void Notifyplatform ();
public interface Displayelemnet {void Display ();
public class Platforma:observerif, displayelemnet {private string title;
private string content;
Private Messagedateif MessageData;
Public Platforma (Messagedateif messagedata) {this.messagedata = MessageData;
public void Update (string title, string content) {this.title = title;
this.content = content;
Display ();
public void Display () {Console.WriteLine ("title:" + title + "Content:" + content); }
}
Summary: Designed for loose coupling between objects. The Observer pattern defines a one-to-many dependency between objects, so that when an object changes state, all its stops are notified and automatically updated