Created
April 19, 2016 04:29
Star
You must be signed in to star a gist
Patterns - Builder Pattern (Real World Example): Separate the construction of a complex object from its representation so that the same construction process can create different representations.
This file contains 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; | |
using System.Collections.Generic; | |
namespace DesignPatterns.Builder.RealWorld | |
{ | |
/// <summary> | |
/// MainApp startup class for Structural | |
/// Builder Design Pattern. | |
/// </summary> | |
public class MainApp | |
{ | |
/// <summary> | |
/// Entry point into console application. | |
/// </summary> | |
/// <param name="args">The arguments.</param> | |
public static void Main(string[] args) | |
{ | |
var waiter = new Waiter(); | |
var hawaiianPizzabuilder = new HawaiianPizzaBuilder(); | |
var spicyPizzabuilder = new SpicyPizzaBuilder(); | |
waiter.SetPizzaBuilder(hawaiianPizzabuilder); | |
waiter.ConstructPizza(); | |
var pizza = waiter.GetPizza(); | |
} | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
class Pizza | |
{ | |
/// <summary> | |
/// The _dough | |
/// </summary> | |
private string _dough = ""; | |
/// <summary> | |
/// The _sauce | |
/// </summary> | |
private string _sauce = ""; | |
/// <summary> | |
/// The _topping | |
/// </summary> | |
private string _topping = ""; | |
/// <summary> | |
/// Sets the dough. | |
/// </summary> | |
/// <param name="dough">The dough.</param> | |
public void SetDough(string dough) | |
{ | |
this._dough = dough; | |
} | |
/// <summary> | |
/// Sets the sauce. | |
/// </summary> | |
/// <param name="sauce">The sauce.</param> | |
public void SetSauce(string sauce) | |
{ | |
this._sauce = sauce; | |
} | |
/// <summary> | |
/// Sets the topping. | |
/// </summary> | |
/// <param name="topping">The topping.</param> | |
public void SetTopping(string topping) | |
{ | |
this._topping = topping; | |
} | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
class Waiter | |
{ | |
/// <summary> | |
/// The _pizza builder | |
/// </summary> | |
private PizzaBuilder _pizzaBuilder; | |
/// <summary> | |
/// Sets the pizza builder. | |
/// </summary> | |
/// <param name="pb">The pb.</param> | |
public void SetPizzaBuilder(PizzaBuilder pb) | |
{ | |
_pizzaBuilder = pb; | |
} | |
/// <summary> | |
/// Gets the pizza. | |
/// </summary> | |
/// <returns></returns> | |
public Pizza GetPizza() | |
{ | |
return _pizzaBuilder.GetPizza(); | |
} | |
/// <summary> | |
/// Constructs the pizza. | |
/// </summary> | |
public void ConstructPizza() | |
{ | |
_pizzaBuilder.CreateNewPizzaProduct(); | |
_pizzaBuilder.BuildDough(); | |
_pizzaBuilder.BuildSauce(); | |
_pizzaBuilder.BuildTopping(); | |
} | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
abstract class PizzaBuilder | |
{ | |
/// <summary> | |
/// The pizza | |
/// </summary> | |
protected Pizza Pizza; | |
/// <summary> | |
/// Gets the pizza. | |
/// </summary> | |
/// <returns></returns> | |
public Pizza GetPizza() | |
{ | |
return Pizza; | |
} | |
/// <summary> | |
/// Creates the new pizza product. | |
/// </summary> | |
public void CreateNewPizzaProduct() | |
{ | |
Pizza = new Pizza(); | |
} | |
/// <summary> | |
/// Builds the dough. | |
/// </summary> | |
public abstract void BuildDough(); | |
/// <summary> | |
/// Builds the sauce. | |
/// </summary> | |
public abstract void BuildSauce(); | |
/// <summary> | |
/// Builds the topping. | |
/// </summary> | |
public abstract void BuildTopping(); | |
} | |
/// <summary> | |
/// ConcreteBuilder | |
/// </summary> | |
/// <seealso cref="DesignPatterns.Builder.RealWorld.PizzaBuilder" /> | |
class HawaiianPizzaBuilder : PizzaBuilder | |
{ | |
/// <summary> | |
/// Builds the dough. | |
/// </summary> | |
public override void BuildDough() | |
{ | |
Pizza.SetDough("cross"); | |
} | |
/// <summary> | |
/// Builds the sauce. | |
/// </summary> | |
public override void BuildSauce() | |
{ | |
Pizza.SetSauce("mild"); | |
} | |
/// <summary> | |
/// Builds the topping. | |
/// </summary> | |
public override void BuildTopping() | |
{ | |
Pizza.SetTopping("ham+pineapple"); | |
} | |
} | |
/// <summary> | |
/// ConcreteBuilder | |
/// </summary> | |
/// <seealso cref="DesignPatterns.Builder.RealWorld.PizzaBuilder" /> | |
class SpicyPizzaBuilder : PizzaBuilder | |
{ | |
/// <summary> | |
/// Builds the dough. | |
/// </summary> | |
public override void BuildDough() | |
{ | |
Pizza.SetDough("pan baked"); | |
} | |
/// <summary> | |
/// Builds the sauce. | |
/// </summary> | |
public override void BuildSauce() | |
{ | |
Pizza.SetSauce("hot"); | |
} | |
/// <summary> | |
/// Builds the topping. | |
/// </summary> | |
public override void BuildTopping() | |
{ | |
Pizza.SetTopping("pepperoni+salami"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment