Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Builder Design Pattern
//Builder design pattern using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { var maker = new SandwichMaker(new MySandwichBuilder()); maker.BuildSandwich(); var sandwich = maker.GetSandwich(); sandwich.Display(); } public class SandwichMaker { private readonly SandwichBuilder builder; public SandwichMaker(SandwichBuilder builder) { this.builder = builder; } public void BuildSandwich() { builder.CreateNewSandwich(); builder.PrepareBread(); builder.AddMeat(); builder.AddChees(); } public Sandwich GetSandwich() { return builder.GetSandwich(); } } public abstract class SandwichBuilder { protected Sandwich sandwich; public void CreateNewSandwich() { sandwich = new Sandwich(); } public Sandwich GetSandwich() { return sandwich; } public abstract void PrepareBread(); public abstract void AddMeat(); public abstract void AddChees(); } public class MySandwichBuilder : SandwichBuilder { public override void PrepareBread() { sandwich.BreadType = BreadType.Black; sandwich.IsGrilled = false; } public override void AddMeat() { sandwich.MeatType = "Szynka"; } public override void AddChees() { sandwich.CheesType = "Cheddar"; } } public class YourSandwichBuilder : SandwichBuilder { public override void PrepareBread() { sandwich.BreadType = BreadType.White; sandwich.IsGrilled = true; } public override void AddMeat() { sandwich.MeatType = "Szynka"; } public override void AddChees() { sandwich.CheesType = "Cheddar"; } } public class Sandwich { public BreadType BreadType{get; set;} public string MeatType{get;set;} public string CheesType{get;set;} public bool IsGrilled{get; set;} public void Display() { Console.WriteLine(BreadType); Console.WriteLine(MeatType); Console.WriteLine(CheesType); if (IsGrilled) Console.WriteLine("Grilled"); else Console.WriteLine("Not grilled"); } } public enum BreadType { White, Black } } }
run
|
edit
|
history
|
help
0
Lab7
adxcsxsd
dima
recursion
ggg
Test Daylight Savings
CubexSoft
Bubble Sort
Properties in Interfaces
Encrypt Decrypt