Skip to content

Instantly share code, notes, and snippets.

@ahmetkucukoglu
ahmetkucukoglu / DeliveredState.cs
Last active November 3, 2023 20:44
ConcreteState - DeliveredState
public class DeliveredState : ShipmentState
{
public override string ToString() => nameof(DeliveredState);
}
@ahmetkucukoglu
ahmetkucukoglu / OutForDeliveryState.cs
Last active November 3, 2023 20:44
ConcreteState - OutForDeliveryState
public class OutForDeliveryState : ShipmentState
{
public override void Delivered(Shipment shipment)
{
Console.WriteLine("The cargo has been delivered.");
shipment.MoveTo(new DeliveredState());
}
public override void NotDelivered(Shipment shipment)
@ahmetkucukoglu
ahmetkucukoglu / CustomsClearedState.cs
Last active November 3, 2023 20:44
ConcreteState - CustomsClearedState
public class CustomsClearedState : ShipmentState
{
public override void OutForDelivery(Shipment shipment)
{
Console.WriteLine("The cargo is out for delivery.");
shipment.MoveTo(new OutForDeliveryState());
}
public override string ToString() => nameof(CustomsClearedState);
@ahmetkucukoglu
ahmetkucukoglu / InCustomsState.cs
Last active November 3, 2023 20:43
ConcreteState - InCustomsState
public class InCustomsState : ShipmentState
{
public override void CustomsCleared(Shipment shipment)
{
Console.WriteLine("Customs clearance procedures have been completed.");
shipment.MoveTo(new CustomsClearedState());
}
public override string ToString() => nameof(InCustomsState);
@ahmetkucukoglu
ahmetkucukoglu / InTransitState.cs
Last active November 3, 2023 20:43
ConcreteState - InTransitState
public class InTransitState : ShipmentState
{
public override void InCustoms(Shipment shipment)
{
Console.WriteLine("The cargo has arrived at customs.");
shipment.MoveTo(new InCustomsState());
}
public override string ToString() => nameof(InTransitState);
@ahmetkucukoglu
ahmetkucukoglu / OrderPlacedState.cs
Last active November 3, 2023 20:43
ConcreteState - OrderPlacedState
public class OrderPlacedState : ShipmentState
{
public override void InTransit(Shipment shipment)
{
if (shipment.FromCountry == "TR")
{
Console.WriteLine(GetInvalidMessage(nameof(InTransit), shipment.State));
return;
}
@ahmetkucukoglu
ahmetkucukoglu / ShipmentState.cs
Created November 3, 2023 19:33
State - ShipmentState
public abstract class ShipmentState
{
public virtual void InTransit(Shipment shipment)
{
Console.WriteLine(GetInvalidMessage(nameof(InTransit), shipment.State));
}
public virtual void InCustoms(Shipment shipment)
{
Console.WriteLine(GetInvalidMessage(nameof(InCustoms), shipment.State));
public class Shipment
{
public Guid OrderId { get; private set; }
public string FromCountry { get; private set; }
public string ToAddress { get; private set; }
public ShipmentStatuses Status { get; private set; }
public int DeliveryAttempts { get; internal set; }
public Shipment(Guid orderId, string fromCountry, string toAddress)
{
@ahmetkucukoglu
ahmetkucukoglu / Demo.cs
Created October 31, 2023 13:08
Demo - ProductService
var product = new Product
{
Id = 1453,
Stock = 0
};
var productService = new ProductService();
productService.Attach(new ProductStockObserver());
product.Stock = 10;
@ahmetkucukoglu
ahmetkucukoglu / ProductService.cs
Created October 31, 2023 13:07
ConcreteSubject - ProductService
public class ProductService : ObservableProduct
{
public void UpdateStock(Product product)
{
//TODO Save product
Notify(new ProductObserverArgs());
}
}