(Real-Life Example: IPacketParser, IDeviceModel, IDatabaseService)”**
Inheritance आपने सीख लिया —
अब आता है Interfaces, जो OOP का सबसे flexible और powerful concept है।
Interfaces आपको allow करते हैं:
Multiple behaviours define करना
Clean architecture बनाना
Loose coupling achieve करना
DI (Dependency Injection) implement करना
Testable code बनाना
Multi-model GPS devices को uniformly handle करना
Packet parser को easily switch करना
GPS servers, APIs, billing systems, microservices —
हर जगह interfaces का heavy use होता है।
Interface =
एक contract / blueprint
जो बताता है कि class में कौन-कौन से methods होने चाहिए।
लेकिन:
❌ कोई implementation नहीं
✔ सिर्फ method definitions
✔ Class को खुद implement करना पड़ता है
Example:
interface ILogger
{
void Log(string message);
}
class ConsoleLogger : ILogger
{
public void Log(string message)
{
Console.WriteLine(message);
}
}
हर model का packet format अलग है:
G17
S15
A20
Concox
Coban
Jimi
सबका parser अलग होगा—
लेकिन method name same होना चाहिए।
Define interface:
interface IPacketParser
{
void Parse(string packet, GPSDevice device);
}
class G17Parser : IPacketParser
{
public void Parse(string packet, GPSDevice device)
{
Console.WriteLine("G17 Packet Parsed");
}
}
class S15Parser : IPacketParser
{
public void Parse(string packet, GPSDevice device)
{
Console.WriteLine("S15 Packet Parsed");
}
}
अब आपका server:
IPacketParser parser = new G17Parser();
parser.Parse(pkt, device);
Parser change करना आसान।
Device table में:
IMEI | Model
-----|--------
8611280... | G17
8611280... | S15
Processor:
IPacketParser parser;
if (model == "G17") parser = new G17Parser();
else parser = new S15Parser();
Clean architecture with interface.
You may have:
SQL DB
MongoDB
FileDB
In-memory store
Define interface:
interface IDatabaseService
{
Task SaveLocationAsync(GPSDevice device);
}
SQL Implementation:
class SqlDbService : IDatabaseService
{
public Task SaveLocationAsync(GPSDevice d)
{
// SQL Code here
}
}
Now swapping DB is simple.
interface IWebSocketService
{
Task PushAsync(GPSDevice device);
}
Use anywhere:
_ws.PushAsync(device);
Different device models may have:
SOS support
Fuel sensor
Temperature support
ACC
Door sensor
Define interface:
interface IDeviceModel
{
bool SupportsSOS { get; }
bool SupportsFuel { get; }
bool SupportsACC { get; }
}
Model:
class G17Model : IDeviceModel
{
public bool SupportsSOS => true;
public bool SupportsFuel => false;
public bool SupportsACC => true;
}
अब features dynamic हो सकते हैं।
Program.cs:
services.AddScoped<IPacketParser, G17Parser>(); services.AddScoped<IDatabaseService, SqlDbService>(); services.AddScoped<IWebSocketService, WsBroadcaster>();
Controller:
public DeviceController(IDatabaseService db)
{
_db = db;
}
This is clean architecture at enterprise level.
| Feature | Interface | Inheritance |
|---|---|---|
| Multiple parents allowed | ✔ Yes | ❌ No |
| Common code share | ❌ No | ✔ Yes |
| Flexibility | ✔ High | Medium |
| Use case | Behaviors | Base class logic |
GPS devices →
Inheritance + Interfaces दोनों का mix best होता है।
✔ जब multiple classes same व्यवहार implement करें
✔ जब DI चाहिए
✔ जब testing आसान बनानी है
✔ जब architecture flexible चाहिए
✔ जब system large हो
C# Interfaces आपके system को बनाते हैं:
✔ Flexible
✔ Testable
✔ Loosely-coupled
✔ Architecture-friendly
✔ GPS server-ready
✔ Clean and scalable
GPS parser, database service, WebSocket server —
हर जगह interface must-have है।
0 Comments
Thanks for Commenting on our blogs, we will revert back with answer of your query.
EmojiThanks & Regards
Sonu Yadav