private static void deliverMessage(IAdvertiser receiver, Message message) { if (!receiver.HandleMessage(message)) { throw new Exception("NPC can't treat the message"); } }
//**** client code that does not need to be changed *** /* * //LDAF007 - THE CORE * Notice that regardless if you pass in BookStoreA or BookStoreB into the method above, this client code does * not need to be changed since it will get the correct advertiser automatically using the internal logics * within the factories. It is the factories (BookStoreA and BookStoreB) that determines which advertiser to produce. * The same goes for choosing which book distributor to produce. * * The benefit of the Abstract Factory pattern is that it allows you to create a groups of products * (the distributors and the advertisers) without having to know the actual class of the product being produced. * The result is that you can have client code that does not need to be changed when the internal logic of * the factories on which product to produce changed. */ public static void Advertise(IBookStore s) { //LDAF004 IAdvertiser a = s.GetAdvertiser(); a.Advertise(); }
public Message(Message other) { sender = other.sender; receiver = other.receiver; msg = (string)other.msg.Clone(); extraInfo = other.extraInfo; }
public Message(IAdvertiser sender, IAdvertiser receiver, string msg, object extraInfo = null) { this.sender = sender; this.receiver = receiver; this.msg = msg; this.extraInfo = extraInfo; }
public IAdvertiser GetAdvertiser() { IAdvertiser advertiser = null; switch (_location) { case CustomerLocation.EastCoast: advertiser = new RedAdvertiser(); break; case CustomerLocation.WestCoast: advertiser = new BlueAdvertiser(); break; default: break; } return(advertiser); }
public static void DispatchMessage(IAdvertiser sender, IAdvertiser receiver, string message, object extraInfo = null) { deliverMessage(receiver, new Message(sender, receiver, message, extraInfo)); }
private static void Advertise(IBookStore bookStore) { IAdvertiser obj = bookStore.GetAdvertiser(); obj.Advertise(); }
//**** client code that does not need to be changed *** private static void Advertise(IBookStore s) { IAdvertiser a = s.GetAdvertiser(); a.Advertise(); }