public Guid Subscribe(Concept observer, string label, EndemeSet enSetRelationship, Endeme enRelationship, EndemeSet enSet, Endeme en) { // -------------------------------------------------------------------------- // Most Concepts will be added to the Observer list // -------------------------------------------------------------------------- EndemeItem standard = new EndemeItem(null); if (standard.ItemKey == Guid.Empty) { throw new Exception("boom"); } if (enRelationship.Count > 1 || !enRelationship.Contains('L')) { standard = Observer.Add(label, enSetRelationship, enRelationship, observer); } if (standard.ItemKey == Guid.Empty) { throw new Exception("boom"); } // -------------------------------------------------------------------------- // A few Concepts will be added to the Friend list // -------------------------------------------------------------------------- if (enRelationship.Contains('L')) { EndemeItem friend = Friend.Add(label, enSet, en, observer); Observer.ChangeGuid(friend.ItemKey, standard.ItemKey); } return(standard.ItemKey); }
public NoticesObserver() { mObserver = new Observer(); mNoticeMapper = new HandlerMapper <int>(); mOnlyOnceListeners = new OnlyOnceListenerMapper(); mObserver.Add(NoticeHandler);//同时侦听来自广播方式派发的消息 }
private static void Main(string[] args) { int num; Pattern pattern; if (args.Length == 0) { Console.WriteLine("Please 0 for mediator, 1 for observer or 2 for factory."); var chosenPattern = Console.ReadLine(); var test = int.TryParse(chosenPattern, out num); if (!test) { Console.WriteLine("You refused to enter a number so we will default to the Mediator."); } } else { var test = int.TryParse(args[0], out num); if (!test) { Console.WriteLine("You refused to enter a number so we will default to the Mediator."); } } switch (num) { case 0: pattern = Pattern.Mediator; break; case 1: pattern = Pattern.Observer; break; case 2: pattern = Pattern.Factory; break; default: pattern = Pattern.Mediator; break; } switch (pattern) { case Pattern.Mediator: IMediator mediator = new ConcreteMediator(); IParticipant participant1 = new ConcreteParticipant1(mediator, "Jay Hardcore"); IParticipant participant2 = new ConcreteParticipant2(mediator, "Chuck"); IParticipant participant3 = new ConcreteParticipant3(mediator, "Brenda"); IParticipant participant4 = new ConcreteParticipant4(mediator, "Hal Jordan"); mediator.AddParticipant(participant1); mediator.AddParticipant(participant2); mediator.AddParticipant(participant3); mediator.AddParticipant(participant4); participant1.SendMessage("Sending message for the first participant"); participant2.SendMessage("Sending message for the second participant"); participant3.SendMessage("Sending message for the third participant"); participant4.SendMessage("Sending message for the fourth participant"); mediator.BroadcastMessage("Mediator message for participant:", participant1); mediator.BroadcastMessage("Mediator message for participant:", participant2); mediator.BroadcastMessage("Mediator message for participant:", participant3); mediator.BroadcastMessage("Mediator message for participant:", participant4); break; case Pattern.Observer: var observable = new Observable(); var anotherObservable = new AnotherObservable(); using (IObserver observer = new Observer(observable, "Tony Stark's Observer")) { Console.WriteLine(observer.GetName()); observer.GetName(); observable.DoSomething(); observer.Add(anotherObservable); anotherObservable.DoSomething(); observer.Remove(observable); observer.Remove(anotherObservable); } break; case Pattern.Factory: new FactoryClient().EnergizeStarShipClient(); break; default: throw new ArgumentOutOfRangeException(); } Console.WriteLine("----------press enter to exit----------"); Console.ReadLine(); }
// ---------------------------------------------------------------------------------------- // IConceptSource Interface implementation // ---------------------------------------------------------------------------------------- /// <summary> /// Implements a method of IConceptSource (similar to IObservable) /// </summary> /// <returns></returns> public Guid Subscribe(Concept observer, string label, EndemeSet enSetRelationship, Endeme enRelationship) { EndemeItem item = Observer.Add(label, enSetRelationship, enRelationship, observer, true); return(item.ItemKey); }