-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLiskovSubstitutionPrincipleStart.cs
96 lines (78 loc) · 2.14 KB
/
LiskovSubstitutionPrincipleStart.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.Text;
namespace SOLID
{
/// <summary>
/// There are three code smells that identify LSP issues:
/// 1. The need to check for a type
/// 2. The need to check for nulls
/// 3. Get a NotImplementedException type
/// </summary>
public class LiskovSubstitutionPrincipleStart
{
private Employee _employee = new Employee();
public void Smell1()
{
var employeeData = _employee.GetEmployee();
if (employeeData.Type == "Manager")
_employee.PrintManager();
_employee.PrintEmployee();
}
public void Smell2()
{
var employeeData = _employee.GetEmployee();
if (employeeData.Id == null)
Console.WriteLine("Employee not found");
_employee.PrintEmployee();
}
public void Smell3()
{
var obj = new DoSomething();
Console.WriteLine(obj.DoSomething2());// will return an exception.
}
}
public class Employee
{
// This breaks the SRP, but it's easier to place all the code in one class for this demo.
public void PrintManager()
{
Console.WriteLine("Im a manager");
}
public void PrintEmployee()
{
Console.WriteLine("Im an employee");
}
public EmployeeModel GetEmployee()
{
return new EmployeeModel()
{
Id = 1,
Name = "Emp Name",
Type = "Manager"
};
}
}
public class EmployeeModel
{
public int? Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
}
public interface IDoSomething
{
string DoSomething1();
string DoSomething2();
}
public class DoSomething: IDoSomething
{
public string DoSomething1()
{
return "Doing something 1";
}
public string DoSomething2()
{
throw new NotImplementedException();
}
}
}