-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
40 lines (32 loc) · 954 Bytes
/
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
var calculatorFactory = new CalculatorFactory();
var adder = calculatorFactory.GetCalculator(CalculateType.Adder);
adder.Calculate(1, 2);
var multiplier = calculatorFactory.GetCalculator(CalculateType.Multiplier);
multiplier.Calculate(4, 2);
public enum CalculateType
{
Default = 0,
Adder = 1,
Multiplier = 2
}
interface ICalculator
{
void Calculate(int val1, int val2);
}
class CalculateMultiplier : ICalculator
{
public void Calculate(int val1, int val2) => Console.WriteLine($"{val1} * {val2} = {val1 * val2}");
}
class CalculateAdder : ICalculator
{
public void Calculate(int val1, int val2) => Console.WriteLine($"{val1} + {val2} = {val1 + val2}");
}
class CalculatorFactory
{
public ICalculator GetCalculator(CalculateType type) => type switch
{
CalculateType.Adder => new CalculateAdder(),
CalculateType.Multiplier => new CalculateMultiplier(),
_ => throw new Exception()
};
}