public void GuestArrives(string guest) { // Now that we have a guest, someone can create a Butler. // It shouldn't be Party that does this. Party shouldn't be responsible for knowing Butler's // friends and family in the first place. But party has the information // and needs a Butler now and there is nobody else around to do it. // Not only that, but we have to construct a whole new butler for each guest that arrives! // We could instead create one of each type and than add an IF THEN IF THEN IF THEN ELSE THEN IF SOMETHING ELSE, // But then why even bother with a creational pattern in the first place? // Here we go. if (new [] { "Billy", "Bobby" }.Contains(guest)) { MyButler = new FriendButler(); } else if (new [] { "Willy", "Wally" }.Contains(guest)) { MyButler = new FamilyButler(); } else { MyButler = new PoliteButler(); } Console.WriteLine("Knock knock!"); MyButler.AnswerDoor(guest); }
static void Main(string[] args) { GreetingsFactory gf = new GreetingsFactory(); Butler b = new Butler(gf); Party p = new Party(b); Console.WriteLine("Who has arrived?"); var guest = Console.ReadLine(); p.GuestArrives(guest); }
public Party(Butler butler) { this.MyButler = butler; }