OOPS concepts with real time example OMS
Order management typically involves processing customer orders, tracking items, calculating prices, handling shipments, and managing customer information.
Real-time Example: Order Management System
Imagine you are building an Order Management System (OMS) or an e-commerce platform. This system allows customers to place orders, track items in their orders, calculate prices, and update order statuses. We'll design classes and methods that demonstrate the core OOP concepts in C#.
___________________________________________________
1. Classes and Objects
Concept: A class defines the structure and behavior of objects. We'll create classes like `Order`, `Customer`, `Product`, and `Payment`.
Real-time example:
csharp
public class Product
{
public string Name { get; set; }
public decimal Price { get; set; }
public Product(string name, decimal price)
{
Name = name;
Price = price;
}
}
public class Order
{
public int OrderId { get; set; }
public List<Product> Products { get; set; }
public decimal TotalAmount { get; set; }
public string Status { get; set; }
public Order(int orderId)
{
OrderId = orderId;
Products = new List<Product>();
Status = "Pending";
}
public void AddProduct(Product product)
{
Products.Add(product);
TotalAmount += product.Price;
}
public void PrintOrderDetails()
{
Console.WriteLine($"Order ID: {OrderId}, Status: {Status}, Total Amount: ${TotalAmount}");
foreach (var product in Products)
{
Console.WriteLine($"- {product.Name}: ${product.Price}");
}
}
}
Usage: csharp
Product product1 = new Product("Laptop", 1000);
Product product2 = new Product("Mouse", 25);
Order order = new Order(1);
order.AddProduct(product1);
order.AddProduct(product2);
order.PrintOrderDetails();
In this example, the `Order` class aggregates `Product` objects, and the `AddProduct` method allows adding items to the order and updating the total price.
___________________________________________________
2. Encapsulation
Concept: Encapsulation involves hiding the internal details and only exposing necessary operations. We can restrict access to the fields (e.g., `TotalAmount`) and provide methods for controlled access.
Real-time example:
csharp
public class Payment
{
private decimal amount;
private string paymentMethod;
public Payment(decimal amount, string paymentMethod)
{
this.amount = amount;
this.paymentMethod = paymentMethod;
}
public void ProcessPayment()
{
Console.WriteLine($"Processing payment of ${amount} using {paymentMethod}");
}
public decimal GetAmount() => amount;
}
Usage:
Payment payment = new Payment(1025, "Credit Card");
payment.ProcessPayment();
In this example, we encapsulate the `amount` and `paymentMethod` in the `Payment` class. The payment is processed through a method (`ProcessPayment`), and the internal state (`amount`) is accessed using a getter method (`GetAmount`).
___________________________________________________
3. Inheritance
Concept: Inheritance allows one class to inherit properties and behaviors from another. This allows for code reuse and extending functionality.
Real-time example:
csharp
public class Customer
{
public string Name { get; set; }
public string Email { get; set; }
public void Register()
{
Console.WriteLine($"{Name} has registered with email {Email}");
}
}
public class PremiumCustomer : Customer
{
public decimal DiscountPercentage { get; set; }
public void ApplyDiscount()
{
Console.WriteLine($"Applying {DiscountPercentage}% discount for Premium customer {Name}");
}
}
Usage:
PremiumCustomer premiumCustomer = new PremiumCustomer { Name = "John Doe", Email = "john@example.com", DiscountPercentage = 10 };
premiumCustomer.Register(); // Inherited method
premiumCustomer.ApplyDiscount(); // Unique to PremiumCustomer
Here, `PremiumCustomer` inherits from `Customer` and adds new functionality (`ApplyDiscount`), demonstrating inheritance in action.
___________________________________________________
4. Polymorphism
Concept: Polymorphism allows objects to be treated as instances of their base class, and methods can behave differently based on the object instance. In C#, polymorphism is commonly implemented using method overriding or interface implementation.
Real-time example (Method Overriding):
public class OrderProcessor
{
public virtual void ProcessOrder(Order order)
{
Console.WriteLine($"Processing regular order with ID: {order.OrderId}");
}
}
public class ExpressOrderProcessor : OrderProcessor
{
public override void ProcessOrder(Order order)
{
Console.WriteLine($"Processing express order with ID: {order.OrderId} - Fast Track");
}
}
Usage:
Order regularOrder = new Order(1);
Order expressOrder = new Order(2);
OrderProcessor processor = new OrderProcessor();
processor.ProcessOrder(regularOrder); // Regular order processing
processor = new ExpressOrderProcessor();
processor.ProcessOrder(expressOrder); // Express order processing
In this case, we override the `ProcessOrder` method in the `ExpressOrderProcessor` class to provide a different implementation for express orders. This is an example of polymorphism, where the same method name behaves differently based on the object's type.
___________________________________________________
5. Abstraction
Concept: Abstraction involves defining a common interface or base class with the essential operations, while hiding complex implementations in derived classes. We use abstract classes or interfaces to achieve abstraction.
Real-time example (Abstract Class):
csharp
public abstract class ShippingMethod
{
public abstract void ShipOrder(Order order);
}
public class StandardShipping : ShippingMethod
{
public override void ShipOrder(Order order)
{
Console.WriteLine($"Shipping order {order.OrderId} using Standard Shipping.");
}
}
public class ExpressShipping : ShippingMethod
{
public override void ShipOrder(Order order)
{
Console.WriteLine($"Shipping order {order.OrderId} using Express Shipping.");
}
}
Usage:
ShippingMethod shippingMethod = new StandardShipping();
shippingMethod.ShipOrder(regularOrder);
shippingMethod = new ExpressShipping();
shippingMethod.ShipOrder(expressOrder);
Here, the `ShippingMethod` class is abstract, and both `StandardShipping` and `ExpressShipping` provide specific implementations. This demonstrates abstraction—the complex details of how shipping is handled are hidden in the subclasses, and the common interface is exposed.
___________________________________________________
6. Interfaces
Concept: An interface defines a contract that a class must adhere to. Unlike abstract classes, interfaces cannot contain any implementation.
Real-time example (Interface):
csharp
public interface IPaymentProcessor
{
void ProcessPayment(decimal amount);
}
public class CreditCardPayment : IPaymentProcessor
{
public void ProcessPayment(decimal amount)
{
Console.WriteLine($"Processing credit card payment of ${amount}");
}
}
public class PayPalPayment : IPaymentProcessor
{
public void ProcessPayment(decimal amount)
{
Console.WriteLine($"Processing PayPal payment of ${amount}");
}
}
Usage:
IPaymentProcessor paymentProcessor = new CreditCardPayment();
paymentProcessor.ProcessPayment(1025);
paymentProcessor = new PayPalPayment();
paymentProcessor.ProcessPayment(1025);
In this example, both `CreditCardPayment` and `PayPalPayment` implement the `IPaymentProcessor` interface, allowing different payment methods to be processed in a uniform way. This is an example of interfaces providing a contract for different classes.
___________________________________________________
Conclusion
In this Order Management System, we've used the following OOP principles to design an efficient and scalable solution:
1. Classes and Objects: Define the entities like `Order`, `Product`, and `Customer`.
2. Encapsulation: Hide internal details like payment amount and restrict access.
3. Inheritance: Reuse and extend functionality with classes like `PremiumCustomer` inheriting from `Customer`.
4. Polymorphism: Provide different implementations for order processing (e.g., regular vs. express orders).
5. Abstraction: Hide shipping details in derived classes like `StandardShipping` and `ExpressShipping`.
6. Interfaces: Define common operations for payment processing via `IPaymentProcessor`.
This design ensures flexibility, maintainability, and the ability to easily extend the system (e.g., adding new shipping methods, payment methods, or order types).
Comments
Post a Comment