Skip to content

Commit 1ceb1a4

Browse files
author
anupavanm
committed
Builder Pattern Example
1 parent 31b2e3d commit 1ceb1a4

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

BuilderPattern/BuilderPattern.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

BuilderPattern/Program.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System;
2+
using System.Text;
3+
4+
namespace BuilderPattern
5+
{
6+
class Program
7+
{
8+
class Burger
9+
{
10+
private int mSize;
11+
private bool mCheese;
12+
private bool mPepperoni;
13+
private bool mLettuce;
14+
private bool mTomato;
15+
16+
public Burger(BurgerBuilder builder)
17+
{
18+
this.mSize = builder.Size;
19+
this.mCheese = builder.Cheese;
20+
this.mPepperoni = builder.Pepperoni;
21+
this.mLettuce = builder.Lettuce;
22+
this.mTomato = builder.Tomato;
23+
}
24+
25+
public string GetDescription()
26+
{
27+
var sb = new StringBuilder();
28+
sb.Append(String.Format("This is {0} inch Burger. ", this.mSize));
29+
return sb.ToString();
30+
}
31+
}
32+
33+
class BurgerBuilder {
34+
public int Size;
35+
public bool Cheese;
36+
public bool Pepperoni;
37+
public bool Lettuce;
38+
public bool Tomato;
39+
40+
public BurgerBuilder(int size)
41+
{
42+
this.Size = size;
43+
}
44+
45+
public BurgerBuilder AddCheese()
46+
{
47+
this.Cheese = true;
48+
return this;
49+
}
50+
51+
public BurgerBuilder AddPepperoni()
52+
{
53+
this.Pepperoni = true;
54+
return this;
55+
}
56+
57+
public BurgerBuilder AddLettuce()
58+
{
59+
this.Lettuce = true;
60+
return this;
61+
}
62+
63+
public BurgerBuilder AddTomato()
64+
{
65+
this.Tomato = true;
66+
return this;
67+
}
68+
69+
public Burger Build()
70+
{
71+
return new Burger(this);
72+
}
73+
}
74+
75+
static void Main(string[] args)
76+
{
77+
var burger = new BurgerBuilder(4).AddCheese()
78+
.AddPepperoni()
79+
.AddLettuce()
80+
.AddTomato()
81+
.Build();
82+
Console.WriteLine(burger.GetDescription());
83+
Console.ReadLine();
84+
}
85+
}
86+
}

DesignPatterns.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FactoryMethodPattern", "Fac
99
EndProject
1010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractFactoryPattern", "AbstractFactoryPattern\AbstractFactoryPattern.csproj", "{081F04C5-CF9E-4997-B04E-5F9FDCCEE90D}"
1111
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BuilderPattern", "BuilderPattern\BuilderPattern.csproj", "{A2804858-D1F9-4849-8C7E-1AB4DA7F979E}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +29,10 @@ Global
2729
{081F04C5-CF9E-4997-B04E-5F9FDCCEE90D}.Debug|Any CPU.Build.0 = Debug|Any CPU
2830
{081F04C5-CF9E-4997-B04E-5F9FDCCEE90D}.Release|Any CPU.ActiveCfg = Release|Any CPU
2931
{081F04C5-CF9E-4997-B04E-5F9FDCCEE90D}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{A2804858-D1F9-4849-8C7E-1AB4DA7F979E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{A2804858-D1F9-4849-8C7E-1AB4DA7F979E}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{A2804858-D1F9-4849-8C7E-1AB4DA7F979E}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{A2804858-D1F9-4849-8C7E-1AB4DA7F979E}.Release|Any CPU.Build.0 = Release|Any CPU
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)