Skip to content

Commit 87b9b78

Browse files
committed
Add code example to Facade pattern
1 parent 5ddf1d4 commit 87b9b78

File tree

11 files changed

+187
-4
lines changed

11 files changed

+187
-4
lines changed

Diagrams/Facade.png

33.6 KB
Loading

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Design Patterns in C# / .NET
44
[![Build Status](https://travis-ci.org/tk-codes/DesignPatterns.NET.svg?branch=master)](https://travis-ci.org/tk-codes/DesignPatterns.NET)
55
![Language C#](https://img.shields.io/badge/language-c%23-blue.svg)
66
![status in progress](https://img.shields.io/badge/status-in%20progress-brightgreen.svg)
7-
![number of patterns](https://img.shields.io/badge/patterns-16-red.svg)
7+
![number of patterns](https://img.shields.io/badge/patterns-17-red.svg)
88

99
## Creational Patterns
1010

@@ -24,7 +24,7 @@ Design Patterns in C# / .NET
2424
| :heavy_check_mark: | [Bridge](/StructuralPatterns/Bridge) |
2525
| :heavy_check_mark: | [Composite](/StructuralPatterns/Composite) |
2626
| :heavy_check_mark: | [Decorator](/StructuralPatterns/Decorator) |
27-
| | [Facade](/StructuralPatterns/Facade) |
27+
| :heavy_check_mark: | [Facade](/StructuralPatterns/Facade) |
2828
| | [Flyweight](/StructuralPatterns/Flyweight) |
2929
| | [Proxy](/StructuralPatterns/Proxy) |
3030

StructuralPatterns/Facade/Checker.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
3+
namespace StructuralPatterns.Facade;
4+
5+
public class Checker
6+
{
7+
private readonly ProgramNode _program;
8+
9+
public Checker(ProgramNode program)
10+
{
11+
_program = program;
12+
}
13+
14+
public bool Check()
15+
{
16+
Console.WriteLine("Checker: Run semantic checks");
17+
18+
return true;
19+
}
20+
}

StructuralPatterns/Facade/Compiler.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
3+
namespace StructuralPatterns.Facade;
4+
5+
// Facade
6+
public class Compiler
7+
{
8+
public void Compile(string sourceCodePath, string targetPath)
9+
{
10+
Console.WriteLine($"Compiler: Starting to compile source code from {sourceCodePath}");
11+
12+
// Subsystem 1
13+
var lexer = new Lexer(sourceCodePath);
14+
15+
// Subsystem 2
16+
var parser = new Parser(lexer);
17+
var program = parser.Parse();
18+
19+
// Subsystem 3
20+
var checker = new Checker(program);
21+
var isSemanticallyCorrect = checker.Check();
22+
23+
// Subsystem 4
24+
if (isSemanticallyCorrect)
25+
{
26+
var generator = new Generator(program);
27+
generator.Generate(targetPath);
28+
}
29+
else
30+
{
31+
Console.WriteLine("Source code has errors");
32+
}
33+
}
34+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
3+
namespace StructuralPatterns.Facade;
4+
5+
public class Generator
6+
{
7+
private readonly ProgramNode _program;
8+
9+
public Generator(ProgramNode program)
10+
{
11+
_program = program;
12+
}
13+
14+
public void Generate(string targetPath)
15+
{
16+
Console.WriteLine($"Generator: Generate IL code at {targetPath}");
17+
}
18+
}

StructuralPatterns/Facade/Lexer.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
3+
namespace StructuralPatterns.Facade;
4+
5+
public class Lexer
6+
{
7+
private readonly string _sourceCodePath;
8+
9+
public Lexer(string sourceCodePath)
10+
{
11+
_sourceCodePath = sourceCodePath;
12+
}
13+
14+
public Token ReadToken()
15+
{
16+
Console.WriteLine("Lexer: Scanning token from source code");
17+
return new Token();
18+
}
19+
}
20+
21+
public class Token{}

StructuralPatterns/Facade/Parser.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
3+
namespace StructuralPatterns.Facade;
4+
5+
public class Parser
6+
{
7+
private readonly Lexer _lexer;
8+
9+
public Parser(Lexer lexer)
10+
{
11+
_lexer = lexer;
12+
}
13+
14+
public ProgramNode Parse()
15+
{
16+
var token = _lexer.ReadToken();
17+
18+
Console.WriteLine("Parser: Parsing the token");
19+
return new ProgramNode();
20+
}
21+
}
22+
23+
public class ProgramNode
24+
{
25+
}

StructuralPatterns/Facade/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,15 @@ Define a `Facade` object that
4343

4444
## Example
4545

46-
TODO
46+
![Facade](../../Diagrams/Facade.png)
47+
48+
**Usage**
49+
50+
```csharp
51+
// Client uses facade which hides the interaction with the sub-processes
52+
var compiler = new Compiler();
53+
compiler.Compile(@"~\hello.cs", @"~\hello.il");
54+
```
4755

4856
## Relations with Other Patterns
4957

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace StructuralPatterns.Facade;
2+
3+
public class TestFacade
4+
{
5+
public static void Run()
6+
{
7+
// Client uses facade which abstracts the interaction with the sub-processes
8+
var compiler = new Compiler();
9+
compiler.Compile(@"~\hello.cs", @"~\hello.il");
10+
}
11+
}

StructuralPatterns/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Program
1010
{
1111
static void Main()
1212
{
13-
Bridge.TestBridge.Run();
13+
Facade.TestFacade.Run();
1414
Console.ReadKey();
1515
}
1616
}

StructuralPatterns/doc/Facade.cd

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<ClassDiagram MajorVersion="1" MinorVersion="1">
3+
<Class Name="StructuralPatterns.Facade.Checker">
4+
<Position X="12" Y="5" Width="1.5" />
5+
<TypeIdentifier>
6+
<HashCode>AAAAAAgAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
7+
<FileName>Facade\Checker.cs</FileName>
8+
</TypeIdentifier>
9+
</Class>
10+
<Class Name="StructuralPatterns.Facade.Compiler">
11+
<Position X="11" Y="2.5" Width="1.5" />
12+
<TypeIdentifier>
13+
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEA=</HashCode>
14+
<FileName>Facade\Compiler.cs</FileName>
15+
</TypeIdentifier>
16+
</Class>
17+
<Class Name="StructuralPatterns.Facade.Generator">
18+
<Position X="14" Y="5" Width="1.5" />
19+
<TypeIdentifier>
20+
<HashCode>AAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAEAAAAA=</HashCode>
21+
<FileName>Facade\Generator.cs</FileName>
22+
</TypeIdentifier>
23+
</Class>
24+
<Class Name="StructuralPatterns.Facade.Lexer">
25+
<Position X="8" Y="5" Width="1.5" />
26+
<TypeIdentifier>
27+
<HashCode>ACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAA=</HashCode>
28+
<FileName>Facade\Lexer.cs</FileName>
29+
</TypeIdentifier>
30+
</Class>
31+
<Class Name="StructuralPatterns.Facade.Parser">
32+
<Position X="10" Y="5" Width="1.5" />
33+
<TypeIdentifier>
34+
<HashCode>AAAgAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAA=</HashCode>
35+
<FileName>Facade\Parser.cs</FileName>
36+
</TypeIdentifier>
37+
</Class>
38+
<Class Name="StructuralPatterns.Facade.TestFacade">
39+
<Position X="7.25" Y="2.5" Width="1.5" />
40+
<TypeIdentifier>
41+
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAI=</HashCode>
42+
<FileName>Facade\TestFacade.cs</FileName>
43+
</TypeIdentifier>
44+
</Class>
45+
<Font Name="Segoe UI" Size="9" />
46+
</ClassDiagram>

0 commit comments

Comments
 (0)