Abstraction is to turn real world objects, properties, and behaviors into a computational representation.
Encapsulation is to protect internal properties and behaviors of an object from being manipulated externally.
Inheritance is to reuse properties and behaviors of a base class by a derived class.
Polymorphism is the ability of an object to assume and be used as other forms.
public abstract record Animal(string Name)
{
public bool Sleeping { get; private set; }
public string Drink() => "Drink";
public string Eat() => "Eat";
public virtual string Move() => "Move";
public abstract string Sound();
public void Awake() => Sleeping = false;
public void Sleep() => Sleeping = true;
}
public sealed record Cat() : Animal(nameof(Cat))
{
public override string Sound() => "Meow";
}
public sealed record Dog() : Animal(nameof(Dog))
{
public override string Sound() => "Bark";
}
public sealed record Duck() : Animal(nameof(Duck))
{
public override string Move() => "Swim";
public override string Sound() => "Quack";
}
public sealed record Eagle() : Animal(nameof(Eagle))
{
public override string Move() => "Fly";
public override string Sound() => "Screech";
}
public sealed record Lion() : Animal(nameof(Lion))
{
public override string Sound() => "Roar";
}
public sealed record Snake() : Animal(nameof(Snake))
{
public override string Sound() => "Hiss";
}
public interface IMessage;
public sealed record EmailMessage(string To, string Body, string Subject) : IMessage;
public sealed record SmsMessage(string To, string Body) : IMessage;
public interface INotification<in TMessage> where TMessage : IMessage
{
void Notify(TMessage message);
}
public sealed class EmailNotification : INotification<EmailMessage>
{
public void Notify(EmailMessage message) => Console.WriteLine(nameof(EmailNotification));
}
public sealed class SmsNotification : INotification<SmsMessage>
{
public void Notify(SmsMessage message) => Console.WriteLine(nameof(SmsNotification));
}
public interface IPayment
{
void Pay(decimal value);
}
public sealed class Cash : IPayment
{
public void Pay(decimal value) => Console.WriteLine(nameof(Cash));
}
public sealed class CreditCard : IPayment
{
public void Pay(decimal value) => Console.WriteLine(nameof(CreditCard));
}
public sealed class DebitCard : IPayment
{
public void Pay(decimal value) => Console.WriteLine(nameof(DebitCard));
}
public sealed class PaymentService(IPayment payment)
{
public void Pay(decimal value) => payment.Pay(value);
}
public interface IRepository<T>
{
void Insert(T entity);
IEnumerable<T> List();
T Select(int id);
void Update(T entity);
}
public abstract class MySqlRepository<T> : IRepository<T>
{
public void Insert(T entity) { }
public IEnumerable<T> List() => new List<T>();
public T Select(int id) => default;
public void Update(T entity) { }
}
public abstract class SqlServerRepository<T> : IRepository<T>
{
public void Insert(T entity) { }
public IEnumerable<T> List() => new List<T>();
public T Select(int id) => default;
public void Update(T entity) { }
}