Skip to content

Latest commit

 

History

History
217 lines (157 loc) · 3.76 KB

readme.md

File metadata and controls

217 lines (157 loc) · 3.76 KB

Object-Oriented Programming

Pillars

Abstraction

Abstraction is to turn real world objects, properties, and behaviors into a computational representation.

Encapsulation

Encapsulation is to protect internal properties and behaviors of an object from being manipulated externally.

Inheritance

Inheritance is to reuse properties and behaviors of a base class by a derived class.

Polymorphism

Polymorphism is the ability of an object to assume and be used as other forms.

Examples

Animal

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
{
    public Cat() : base(nameof(Cat)) { }

    public override string Sound() => "Meow";
}
public sealed record Dog : Animal
{
    public Dog() : base(nameof(Dog)) { }

    public override string Sound() => "Bark";
}
public sealed record Duck : Animal
{
    public Duck() : base(nameof(Duck)) { }

    public override string Move() => "Swim";

    public override string Sound() => "Quack";
}
public sealed record Eagle : Animal
{
    public Eagle() : base(nameof(Eagle)) { }

    public override string Move() => "Fly";

    public override string Sound() => "Screech";
}
public sealed record Lion : Animal
{
    public Lion() : base(nameof(Lion)) { }

    public override string Sound() => "Roar";
}
public sealed record Snake : Animal
{
    public Snake() : base(nameof(Snake)) { }

    public override string Sound() => "Hiss";
}

Notification

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<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));
}

Payment

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
{
    public PaymentService(IPayment payment) => Payment = payment;

    private IPayment Payment { get; }

    public void Pay(decimal value) => Payment.Pay(value);
}

Repositories

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) { }
}