/
OpenClosed.cs
54 lines (44 loc) · 1.11 KB
/
OpenClosed.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
using UnityEngine;
namespace SOLIDPrinciples.OpenClosed
{
public class OpenClosed : MonoBehaviour
{
// Software entities (classes, modules, functions, components, etc.)
// should be open for extension, but closed for modifications.
private void Start()
{
Square square = new Square(5);
Circle circle = new Circle(3);
Debug.Log("Area = " + square.GetArea());
Debug.Log("Area = " + circle.GetArea());
}
}
public abstract class Shape
{
public abstract float GetArea();
}
public class Square : Shape
{
private float _length;
public Square(float length)
{
_length = length;
}
public override float GetArea()
{
return Mathf.Pow(_length, 2);
}
}
public class Circle : Shape
{
private float _radius;
public Circle(float radius)
{
_radius = radius;
}
public override float GetArea()
{
return Mathf.PI * Mathf.Pow(_radius, 2);
}
}
}