Created
May 25, 2010 23:46
-
-
Save njonsson/413840 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mintonsPlayhouseCirca1941 = new Nightclub(); | |
mintonsPlayhouseCirca1941.Bouncer = delegate(person) | |
{ | |
// Welcome, paying customers! | |
var customer = person as ICustomer; | |
if ((customer != null) && (customer.CashOnHand > 0)) return true; | |
// Feel free to sit in ... if you can play, that is. | |
var musician = person as IMusician; | |
if ((musician != null) && | |
musician.CanPlay(Standards.Perdido.Transpose(Key.CSharpMajor))) | |
{ | |
return true; | |
} | |
// "Get outta here, and get me some money, too." | |
// http://en.wikipedia.org/wiki/Why_Don't_You_Do_Right%3F | |
return false; | |
}; | |
mintonsPlayhouseCirca1941.Enter(dizzyGillespie); | |
// => true | |
mintonsPlayhouseCirca1941.Enter(woodyGuthrie); | |
// => false | |
mintonsPlayhouseCirca1941.Enter(daddyWarbucks); | |
// => true | |
mintonsPlayhouseCirca1941.Enter(pennilessHobo); | |
// => false | |
System.Console.WriteLine(mintonsPlayhouseCirca1941.Occupants); | |
// => new Person[] { dizzyGillespie, daddyWarbucks } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
class Nightclub | |
{ | |
public delegate bool IsAllowed(Person person); | |
private readonly List<Person> occupants = new List<Person>(); | |
public Nightclub() | |
{ | |
occupants = new List<Person>(); | |
} | |
public IsAllowed Bouncer { get; set; } | |
public Person[] Occupants | |
{ | |
get { return occupants.ToArray(); } | |
} | |
public bool Enter(Person person) | |
{ | |
if ((Bouncer != null) && !Bouncer(person)) return false; | |
occupants.Add(person); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment