Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rafaelfgx/objectorientedprogramming
Object-Oriented Programming.
https://github.com/rafaelfgx/objectorientedprogramming
oop oop-principles
Last synced: 3 months ago
JSON representation
Object-Oriented Programming.
- Host: GitHub
- URL: https://github.com/rafaelfgx/objectorientedprogramming
- Owner: rafaelfgx
- License: mit
- Created: 2019-08-22T01:00:24.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-11-12T21:13:31.000Z (3 months ago)
- Last Synced: 2024-11-12T22:22:00.315Z (3 months ago)
- Topics: oop, oop-principles
- Language: C#
- Homepage:
- Size: 6.84 KB
- Stars: 24
- Watchers: 4
- Forks: 12
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.md
Awesome Lists containing this project
README
# 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
```cs
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;
}
``````cs
public sealed record Cat : Animal
{
public Cat() : base(nameof(Cat)) { }public override string Sound() => "Meow";
}
``````cs
public sealed record Dog : Animal
{
public Dog() : base(nameof(Dog)) { }public override string Sound() => "Bark";
}
``````cs
public sealed record Duck : Animal
{
public Duck() : base(nameof(Duck)) { }public override string Move() => "Swim";
public override string Sound() => "Quack";
}
``````cs
public sealed record Eagle : Animal
{
public Eagle() : base(nameof(Eagle)) { }public override string Move() => "Fly";
public override string Sound() => "Screech";
}
``````cs
public sealed record Lion : Animal
{
public Lion() : base(nameof(Lion)) { }public override string Sound() => "Roar";
}
``````cs
public sealed record Snake : Animal
{
public Snake() : base(nameof(Snake)) { }public override string Sound() => "Hiss";
}
```### Notification
```cs
public interface IMessage { }
``````cs
public sealed record EmailMessage(string To, string Body, string Subject) : IMessage;
``````cs
public sealed record SmsMessage(string To, string Body) : IMessage;
``````cs
public interface INotification where TMessage : IMessage
{
void Notify(TMessage message);
}
``````cs
public sealed class EmailNotification : INotification
{
public void Notify(EmailMessage message) => Console.WriteLine(nameof(EmailNotification));
}
``````cs
public sealed class SmsNotification : INotification
{
public void Notify(SmsMessage message) => Console.WriteLine(nameof(SmsNotification));
}
```### Payment
```cs
public interface IPayment
{
void Pay(decimal value);
}
``````cs
public sealed class Cash : IPayment
{
public void Pay(decimal value) => Console.WriteLine(nameof(Cash));
}
``````cs
public sealed class CreditCard : IPayment
{
public void Pay(decimal value) => Console.WriteLine(nameof(CreditCard));
}
``````cs
public sealed class DebitCard : IPayment
{
public void Pay(decimal value) => Console.WriteLine(nameof(DebitCard));
}
``````cs
public sealed class PaymentService
{
public PaymentService(IPayment payment) => Payment = payment;private IPayment Payment { get; }
public void Pay(decimal value) => Payment.Pay(value);
}
```### Repositories
```cs
public interface IRepository
{
void Insert(T entity);IEnumerable List();
T Select(int id);
void Update(T entity);
}
``````cs
public abstract class MySqlRepository : IRepository
{
public void Insert(T entity) { }public IEnumerable List() => new List();
public T Select(int id) => default;
public void Update(T entity) { }
}
``````cs
public abstract class SqlServerRepository : IRepository
{
public void Insert(T entity) { }public IEnumerable List() => new List();
public T Select(int id) => default;
public void Update(T entity) { }
}
```