-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
132 lines (113 loc) · 2.7 KB
/
Program.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* Прототип
Определяет несколько видов объектов,
чтобы при создании использовать объект-прототип
и создаёт новые объекты, копируя прототип
(техника клонирования объектов)
*/
class Program
{
static void Main()
{
#region Пример №1 - базовое
IFigure cube = new Cube(10, 10);
IFigure cloneCube = cube.Clone();
IFigure cloneMemberCube = cube.CloneMember();
cube.GetInfo();
cloneCube.GetInfo();
cloneMemberCube.GetInfo();
#endregion
#region Пример №2
IEntity entity = new Predator(10, 1000);
var clone = entity.CloneEntity();
entity.GetInfo();
clone.GetInfo();
Console.ReadKey();
#endregion
}
}
/// <summary>
/// Поведение фигуры
/// </summary>
interface IFigure
{
IFigure Clone();
IFigure? CloneMember();
void GetInfo();
}
/// <summary>
/// Шаблон куба
/// </summary>
class Cube : IFigure
{
int Width { get; set; }
int Height { get; set; }
public Cube(int width, int heght)
{
Width = width;
Height = heght;
}
public IFigure Clone()
{
return new Cube(Width, Height);
}
public IFigure? CloneMember()
{
return this.MemberwiseClone() as IFigure;
}
public void GetInfo()
{
Console.WriteLine($"Cube {Width}/{Height}");
}
}
/// <summary>
/// Треугольник
/// </summary>
class Rect : IFigure
{
int Width { get; set; }
int Height { get; set; }
public Rect(int width, int heght)
{
Width = width;
Height = heght;
}
public IFigure Clone()
{
return new Rect(Width, Height);
}
public IFigure? CloneMember()
{
return this.MemberwiseClone() as IFigure;
}
public void GetInfo()
{
Console.WriteLine($"Rect {Width}/{Height}");
}
}
/// <summary>
/// Поведение сущности
/// </summary>
interface IEntity
{
IEntity CloneEntity();
void GetInfo();
}
abstract record Animal(int Paws, int Weight) : IEntity
{
public abstract IEntity CloneEntity();
public abstract void GetInfo();
}
record Predator(int Paws, int Weight) : Animal(Paws, Weight)
{
public override IEntity CloneEntity()
=> this with { };
public override void GetInfo()
=> Console.WriteLine($"Predator w={Weight} p={Paws}");
}
record Person(int IQ) : IEntity
{
public IEntity CloneEntity()
=> this with { };
public void GetInfo()
=> Console.WriteLine($"Peaple with IQ={IQ}");
}