-
Notifications
You must be signed in to change notification settings - Fork 0
/
Factoyr.cs
113 lines (86 loc) · 2.21 KB
/
Factoyr.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace Singleton
{
//2. Factory Pattern
// Idea: Imagine a toy factory where you give it a type of toy you want,
// like a car or a doll, and it gives you that toy.
// The factory pattern helps us create objects without specifying the exact class of object that will be created
internal class Factory
{
}
public abstract class Toy
{
public abstract void Play();
}
public class Car : Toy
{
public override void Play()
{
Console.WriteLine("Car Created... Vroom Vroom ");
}
}
public class Doll : Toy
{
public override void Play()
{
Console.WriteLine("Doll Created... Dance Dabce");
}
}
public class ToyFactory
{
public static Toy CreateToy(string toyname)
{
if (toyname.ToUpper() == "Car".ToUpper())
{
return new Car();
}
else if (toyname.ToUpper() == "Doll".ToUpper())
{
return new Doll();
}
else
{
throw new ArgumentException($"'{nameof(toyname)}' cannot be null or empty.", nameof(toyname));
}
}
}
public interface IVehicle
{
void Describe();
}
public class Bike : IVehicle
{
public void Describe()
{
Console.WriteLine("BIke Created");
}
}
public class Cycle : IVehicle
{
public void Describe()
{
Console.WriteLine("Cycle Created");
}
}
public class VehicleFactory
{
public static IVehicle getVechicle(string name)
{
switch (name.ToLower())
{
case "bike":
return new Bike();
case "cycle":
return new Cycle();
default:
throw new ArgumentException("Invalid vehicle type.");
}
}
}
}