Skip to content

Commit 73875d4

Browse files
author
anupavanm
committed
Bridge Pattern example
1 parent ccb6465 commit 73875d4

File tree

4 files changed

+184
-65
lines changed

4 files changed

+184
-65
lines changed

BridgePattern/Program.cs

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,81 @@
22

33
namespace BridgePattern
44
{
5-
class Program
6-
{
7-
interface IWebPage
8-
{
9-
string GetContent();
10-
}
5+
class Program
6+
{
7+
interface IWebPage
8+
{
9+
string GetContent();
10+
}
1111

12-
class About : IWebPage
13-
{
14-
protected ITheme theme;
12+
class About : IWebPage
13+
{
14+
protected ITheme theme;
1515

16-
public About(ITheme theme)
17-
{
18-
this.theme = theme;
19-
}
16+
public About(ITheme theme)
17+
{
18+
this.theme = theme;
19+
}
2020

21-
public string GetContent()
22-
{
23-
return String.Format("About page in {0}",theme.GetColor());
24-
}
25-
}
21+
public string GetContent()
22+
{
23+
return String.Format("About page in {0}", theme.GetColor());
24+
}
25+
}
2626

27-
class Careers : IWebPage
28-
{
29-
protected ITheme theme;
27+
class Careers : IWebPage
28+
{
29+
protected ITheme theme;
3030

31-
public Careers(ITheme theme)
32-
{
33-
this.theme = theme;
34-
}
31+
public Careers(ITheme theme)
32+
{
33+
this.theme = theme;
34+
}
3535

36-
public string GetContent()
37-
{
38-
return String.Format("Careers page in {0}",theme.GetColor());
39-
}
40-
}
36+
public string GetContent()
37+
{
38+
return String.Format("Careers page in {0}", theme.GetColor());
39+
}
40+
}
4141

42-
interface ITheme
43-
{
44-
string GetColor();
45-
}
42+
interface ITheme
43+
{
44+
string GetColor();
45+
}
4646

47-
class DarkTheme : ITheme
48-
{
49-
public string GetColor()
50-
{
51-
return "Dark Black";
52-
}
53-
}
47+
class DarkTheme : ITheme
48+
{
49+
public string GetColor()
50+
{
51+
return "Dark Black";
52+
}
53+
}
5454

55-
class LightTheme : ITheme
56-
{
57-
public string GetColor()
58-
{
59-
return "Off White";
60-
}
61-
}
55+
class LightTheme : ITheme
56+
{
57+
public string GetColor()
58+
{
59+
return "Off White";
60+
}
61+
}
6262

63-
class AquaTheme : ITheme
64-
{
65-
public string GetColor()
66-
{
67-
return "Light blue";
68-
}
69-
}
70-
static void Main(string[] args)
71-
{
72-
var darkTheme = new DarkTheme();
63+
class AquaTheme : ITheme
64+
{
65+
public string GetColor()
66+
{
67+
return "Light blue";
68+
}
69+
}
70+
static void Main(string[] args)
71+
{
72+
var darkTheme = new DarkTheme();
7373

74-
var about= new About(darkTheme);
75-
var careers = new Careers(darkTheme);
74+
var about = new About(darkTheme);
75+
var careers = new Careers(darkTheme);
7676

77-
Console.WriteLine(about.GetContent());
78-
Console.WriteLine(careers.GetContent());
79-
Console.ReadLine();
80-
}
81-
}
77+
Console.WriteLine(about.GetContent());
78+
Console.WriteLine(careers.GetContent());
79+
Console.ReadLine();
80+
}
81+
}
8282
}
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>

CompositePattern/Program.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace CompositePattern
5+
{
6+
interface IEmployee
7+
{
8+
float GetSalary();
9+
string GetRole();
10+
string GetName();
11+
}
12+
13+
14+
class Developer : IEmployee
15+
{
16+
private string mName;
17+
private float mSalary;
18+
19+
public Developer(string name, float salary)
20+
{
21+
this.mName = name;
22+
this.mSalary = salary;
23+
}
24+
25+
public float GetSalary()
26+
{
27+
return this.mSalary;
28+
}
29+
30+
public string GetRole()
31+
{
32+
return "Developer";
33+
}
34+
35+
public string GetName()
36+
{
37+
return this.mName;
38+
}
39+
}
40+
41+
class Designer : IEmployee
42+
{
43+
private string mName;
44+
private float mSalary;
45+
46+
public Designer(string name, float salary)
47+
{
48+
this.mName = name;
49+
this.mSalary = salary;
50+
}
51+
52+
public float GetSalary()
53+
{
54+
return this.mSalary;
55+
}
56+
57+
public string GetRole()
58+
{
59+
return "Designer";
60+
}
61+
62+
public string GetName()
63+
{
64+
return this.mName;
65+
}
66+
}
67+
class Organization
68+
{
69+
protected List<IEmployee> employees;
70+
71+
public Organization()
72+
{
73+
employees = new List<IEmployee>();
74+
}
75+
76+
public void AddEmployee(IEmployee employee)
77+
{
78+
employees.Add(employee);
79+
}
80+
81+
public float GetNetSalaries()
82+
{
83+
float netSalary = 0;
84+
85+
foreach (var e in employees) {
86+
netSalary += e.GetSalary();
87+
}
88+
return netSalary;
89+
}
90+
}
91+
class Program
92+
{
93+
static void Main(string[] args)
94+
{
95+
var developer = new Developer("John", 5000);
96+
var designer = new Designer("Arya", 5000);
97+
98+
var organization = new Organization();
99+
organization.AddEmployee(developer);
100+
organization.AddEmployee(designer);
101+
Console.WriteLine("Net Salary of Emmployees in Organization is {0:c}", organization.GetNetSalaries());
102+
Console.ReadLine();
103+
}
104+
}
105+
}

DesignPatterns.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdapterPattern", "AdapterPa
1717
EndProject
1818
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BridgePattern", "BridgePattern\BridgePattern.csproj", "{8F9283E5-7EE1-4125-9CFB-FB6C380240D5}"
1919
EndProject
20+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompositePattern", "CompositePattern\CompositePattern.csproj", "{3CFAA453-5386-4A9E-9DDA-20A0DC8F700D}"
21+
EndProject
2022
Global
2123
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2224
Debug|Any CPU = Debug|Any CPU
@@ -51,6 +53,10 @@ Global
5153
{8F9283E5-7EE1-4125-9CFB-FB6C380240D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
5254
{8F9283E5-7EE1-4125-9CFB-FB6C380240D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
5355
{8F9283E5-7EE1-4125-9CFB-FB6C380240D5}.Release|Any CPU.Build.0 = Release|Any CPU
56+
{3CFAA453-5386-4A9E-9DDA-20A0DC8F700D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
57+
{3CFAA453-5386-4A9E-9DDA-20A0DC8F700D}.Debug|Any CPU.Build.0 = Debug|Any CPU
58+
{3CFAA453-5386-4A9E-9DDA-20A0DC8F700D}.Release|Any CPU.ActiveCfg = Release|Any CPU
59+
{3CFAA453-5386-4A9E-9DDA-20A0DC8F700D}.Release|Any CPU.Build.0 = Release|Any CPU
5460
EndGlobalSection
5561
GlobalSection(SolutionProperties) = preSolution
5662
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)