Skip to content

Instantly share code, notes, and snippets.

@JamesTryand
Last active May 27, 2016 22:03
Show Gist options
  • Save JamesTryand/464a2f177ec59c7910ae96cf3fb6d510 to your computer and use it in GitHub Desktop.
Save JamesTryand/464a2f177ec59c7910ae96cf3fb6d510 to your computer and use it in GitHub Desktop.
builder pattern example.
//
namespace Badger {
public class WhereYouNeedAThing {
public Growl Lalala(string thing, Person customer, NoiseMaker speakers) {
return new KindOfGrowl().WithA(thing).ForThe(customer).AmplifiedBy(speakers) as Growl;
}
}
public class Growl {
public Growl(string resource, Person subject, NoiseMaker noisything) {
Thing = resource;
Subject = subject;
NoisyThing = noisyThing;
}
string Thing { get; }
Person Subject { get; }
NoiseMaker NoisyThing { get; }
}
public class KindOfGrowl {
public KindOfGrowl WithA(string thing) {
this.thing = thing;
return this;
}
public KindOfGrowl ForThe(Person customer) {
this.subject = customer;
return this;
}
public KindOfGrowl AmplifiedBy(NoiseMaker noisyThing){
this.noisyThing = noisyThing;
return this;
}
private string thing;
private Person subject;
private NoiseMaker noisyThing;
public static implicit operator Growl(KindOfGrowl yeahThis) {
return new Growl(thing, subject, noisyThing);
}
}
// stub helper classes for the example - ignore them here
public class Person { }
public class NoiseMaker { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment