-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest
More file actions
275 lines (231 loc) · 7.51 KB
/
Copy pathtest
File metadata and controls
275 lines (231 loc) · 7.51 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
using System;
// Class responsible for generating messages
public class MessageGenerator
{
// Method to generate a simple message
public string GenerateMessage()
{
// Return a simple message string
return "Hello, this is a simple message!";
}
}
// Class responsible for printing messages
public class MessagePrinter
{
// Method to print a message to the console
public void PrintMessage(string message)
{
// Print the message to the console
Console.WriteLine(message);
}
}
// Main program class
class Program
{
static void Main(string[] args)
{
// Create an instance of MessageGenerator
MessageGenerator messageGenerator = new MessageGenerator();
// Generate a message using the MessageGenerator instance
string message = messageGenerator.GenerateMessage();
// Create an instance of MessagePrinter
MessagePrinter messagePrinter = new MessagePrinter();
// Print the generated message using the MessagePrinter instance
messagePrinter.PrintMessage(message);
}
}
using System;
using System.Collections.Generic;
// Abstract base class representing a generic shape
public abstract class Shape
{
// Abstract method to calculate the area of the shape
public abstract double Area();
}
// Class representing a circle, derived from Shape
public class Circle : Shape
{
// Property to store the radius of the circle
public double Radius { get; set; }
// Override the Area method to calculate the area of the circle
public override double Area() => Math.PI * Radius * Radius;
}
// Class representing a rectangle, derived from Shape
public class Rectangle : Shape
{
// Property to store the width of the rectangle
public double Width { get; set; }
// Property to store the height of the rectangle
public double Height { get; set; }
// Override the Area method to calculate the area of the rectangle
public override double Area() => Width * Height;
}
// Main program class
class Program
{
static void Main(string[] args)
{
// Create a list to store different shapes
List<Shape> shapes = new List<Shape>();
// Add a circle with a radius of 5 to the list
shapes.Add(new Circle { Radius = 5 });
// Add a rectangle with a width of 4 and height of 6 to the list
shapes.Add(new Rectangle { Width = 4, Height = 6 });
// Iterate through each shape in the list
foreach (var shape in shapes)
{
// Print the type of the shape and its area
Console.WriteLine($"Shape: {shape.GetType().Name}, Area: {shape.Area()}");
}
}
}
using System;
using System.Collections.Generic;
// Interface representing flyable behavior
public interface IFlyable
{
void Fly();
}
// Base class representing a generic bird
public abstract class Bird
{
// Common properties or methods for all birds can be added here
}
// Class representing a sparrow, derived from Bird and implementing IFlyable
public class Sparrow : Bird, IFlyable
{
// Implement the Fly method for sparrows
public void Fly() => Console.WriteLine("Sparrow is flying");
}
// Class representing an ostrich, derived from Bird
public class Ostrich : Bird
{
// Ostriches do not implement IFlyable because they cannot fly
}
// Main program class
class Program
{
static void Main(string[] args)
{
// Create a list to store different birds
List<Bird> birds = new List<Bird>();
// Add a sparrow to the list
birds.Add(new Sparrow());
// Add an ostrich to the list
birds.Add(new Ostrich());
// Iterate through each bird in the list
foreach (var bird in birds)
{
// Check if the bird can fly
if (bird is IFlyable flyableBird)
{
// Make the bird fly
flyableBird.Fly();
}
else
{
// Handle birds that cannot fly
Console.WriteLine($"{bird.GetType().Name} can't fly");
}
}
}
}
using System;
// Interface for printing functionality
public interface IPrinter
{
// Method to print documents
void Print();
}
// Interface for scanning functionality
public interface IScanner
{
// Method to scan documents
void Scan();
}
// Class representing a multi-function printer that implements both IPrinter and IScanner
public class MultiFunctionPrinter : IPrinter, IScanner
{
// Implement the Print method from IPrinter
public void Print() => Console.WriteLine("Printing");
// Implement the Scan method from IScanner
public void Scan() => Console.WriteLine("Scanning");
}
// Class representing a simple printer that only implements IPrinter
public class SimplePrinter : IPrinter
{
// Implement the Print method from IPrinter
public void Print() => Console.WriteLine("Printing");
}
// Class representing a simple scanner that only implements IScanner
public class SimpleScanner : IScanner
{
// Implement the Scan method from IScanner
public void Scan() => Console.WriteLine("Scanning");
}
// Main program class
class Program
{
static void Main(string[] args)
{
// Create an instance of MultiFunctionPrinter
MultiFunctionPrinter mfp = new MultiFunctionPrinter();
mfp.Print(); // Output: Printing
mfp.Scan(); // Output: Scanning
// Create an instance of SimplePrinter
SimplePrinter printer = new SimplePrinter();
printer.Print(); // Output: Printing
// Create an instance of SimpleScanner
SimpleScanner scanner = new SimpleScanner();
scanner.Scan(); // Output: Scanning
}
}
using System;
// Interface representing a message sender
public interface IMessageSender
{
// Method to send a message
void SendMessage(string message);
}
// Class representing an email sender, implementing IMessageSender
public class EmailSender : IMessageSender
{
// Implement the SendMessage method to send an email
public void SendMessage(string message) => Console.WriteLine("Email sent: " + message);
}
// Class representing an SMS sender, implementing IMessageSender
public class SmsSender : IMessageSender
{
// Implement the SendMessage method to send an SMS
public void SendMessage(string message) => Console.WriteLine("SMS sent: " + message);
}
// High-level module representing a notification
public class Notification
{
// Dependency on the abstraction IMessageSender
private readonly IMessageSender _messageSender;
// Constructor to inject the dependency
public Notification(IMessageSender messageSender)
{
_messageSender = messageSender;
}
// Method to send a notification
public void Send(string message) => _messageSender.SendMessage(message);
}
// Main program class
class Program
{
static void Main(string[] args)
{
// Create an instance of EmailSender
IMessageSender emailSender = new EmailSender();
// Create an instance of Notification with EmailSender
Notification emailNotification = new Notification(emailSender);
emailNotification.Send("Hello via Email!"); // Output: Email sent: Hello via Email!
// Create an instance of SmsSender
IMessageSender smsSender = new SmsSender();
// Create an instance of Notification with SmsSender
Notification smsNotification = new Notification(smsSender);
smsNotification.Send("Hello via SMS!"); // Output: SMS sent: Hello via SMS!
}
}