Programming paradigms - Procedural vs Object Oriented Programming

Publish date: 2024-06-21
Tags: System-Design, Software-Engineering

Programming paradigms

Types of Programming paradigms

Imperative:

Declarative:

Imperative

Procedural programming:

Python Procedural Code

 def transfer(source: int, destination: int, amount: int) -> None:
    source_account = get_account(source)
    update_account(source_account, -amount)
    destination_account = get_account(destination)
    update_account(destination_account, amount)

def get_account(number: int) -> dict:
    return list(filter(lambda account: account['number'] == number,accounts))[0]

def update_account(account: int, delta: int) -> None:
    account['balance'] += delta

Object Oriented Programming

Abstraction:

Advantages of Abstraction

Encapsulation:

Advantages of Encapsulation:

Class:

Object:

Java OOP Code

public class OopBankAccount {
    private Integer number;
    private Integer balance;
    
    public OopBankAccount(Integer number, Integer balance) {
        this.number = number;
        this.balance = balance;
    }
    
    void deposit(Integer amount) {
        this.balance += amount;
    }
    
    void withdraw(Integer amount) {
        this.balance += amount;
    }
    
    void transfer(OopBankAccount destination, Integer amount) {
        this.withdraw(amount);
        destination.deposit(amount);
    }
}

Python OOP Code

class OopBankAccount:
	def __init__(self, balance, number):
		self.__number = number
		self.__balance = balance

	def getNumber(self):
		return self.__number

	def setNumber(self, number):
		self.__number = number 

	def getBalance(self):
		return self.__balance

	def setBalance(self, balance):
		self.__balance = balance

	def deposit(self, amount):
		self.__balance += amount

	def withdraw(self, amount):
		self.__balance -= amount

	def transfer(self, destination, amount):
		self.withdraw(amount)
		destination.deposit(amount)

Golang Code

type BankAccount struct {
	AccountNumber int64
	Name          string
	Balance       float64
}

type BankAccountOps interface {
	GetAccountNumber() (int64, error)
	GetName() (string, error)
	GetBalance() (float64, error)
	Deposit(amount float64) error
	Withdraw(amount float64) error
	Transfer(destiantion *BankAccount, amount float64) error
	PrintBalance()
}

func NewBankAccount(number int64, name string, balance float64) *BankAccount {
	return &BankAccount{AccountNumber: number, Name: name, Balance: balance}
}

func (b *BankAccount) GetAccountNumber() (int64, error) {
	return b.AccountNumber, nil
}

func (b *BankAccount) GetName() (string, error) {
	return b.Name, nil
}

func (b *BankAccount) GetBalance() (float64, error) {
	return b.Balance, nil
}

func (b *BankAccount) Deposit(amount float64) error {
	b.Balance += amount
	return nil
}

func (b *BankAccount) Withdraw(amount float64) error {
	b.Balance -= amount
	return nil
}

func (b *BankAccount) Transfer(destiantion *BankAccount, amount float64) error {
	b.Withdraw(amount)
	destiantion.Deposit(amount)

	return nil
}

func (b *BankAccount) PrintBalance() {
	fmt.Println(b.AccountNumber, b.Name, b.Balance)
}

Advantages:

Disadvantages:

These are my notes from the Low-Level Design (LLD) course I took at Scaler.

Check Python, Java and Go code on Github Repo

Tags: System-Design, Software-Engineering