DEV Community

Sajjad Rahman
Sajjad Rahman

Posted on

Core oop

চারটি Core OOP Concepts হলো:

  • Encapsulation

  • Inheritance

  • Polymorphism

  • Abstraction

তোমার প্রজেক্টে কোনটি কোথায় ব্যবহার করেছ, দেখি।

1. OOP Core Concepts Mapping

1. Encapsulation

Included

কোথায়: BankAccount class

  • Account balance সরাসরি পরিবর্তন করা যাবে না।

  • _balance বা __balance দিয়ে attribute protect করা।

  • deposit() এবং withdraw() methods দিয়ে balance পরিবর্তন।

  • @property এবং setter ব্যবহার।

class BankAccount:
    def __init__(self, balance):
        self.__balance = balance

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

    def get_balance(self):
        return self.__balance
Enter fullscreen mode Exit fullscreen mode

2. Inheritance

Included

কোথায়: Account, SavingsAccount, CurrentAccount classes

  • Account হবে Parent class।

  • SavingsAccount হবে Child class।

  • CurrentAccount হবে Child class।

  • super() ব্যবহার করে parent class-এর functionality reuse করা।

class Account:
    def __init__(self, balance):
        self.balance = balance

class SavingsAccount(Account):
    pass

class CurrentAccount(Account):
    pass
Enter fullscreen mode Exit fullscreen mode

3. Polymorphism

Included

কোথায়: SavingsAccount এবং CurrentAccount

একই method, কিন্তু account অনুযায়ী ভিন্ন behaviour।

class SavingsAccount(Account):
    def withdraw(self, amount):
        print("Savings withdrawal rules")

class CurrentAccount(Account):
    def withdraw(self, amount):
        print("Current withdrawal rules")
Enter fullscreen mode Exit fullscreen mode

এখানে একই withdraw() method দুই ধরনের account-এ ভিন্নভাবে কাজ করবে।

4. Abstraction

Included

কোথায়: Account abstract class

  • ABC ব্যবহার করবে।

  • @abstractmethod ব্যবহার করবে।

  • Child classes-কে নির্দিষ্ট methods implement করতে বাধ্য করবে।

from abc import ABC, abstractmethod

class Account(ABC):

    @abstractmethod
    def withdraw(self, amount):
        pass

    @abstractmethod
    def calculate_interest(self):
        pass
Enter fullscreen mode Exit fullscreen mode

2. তোমার Project-এ OOP Class Structure

এভাবে তোমার class relationship সাজানো যেতে পারে:

                 Account (ABC)
                      |
             -------------------
             |                 |
       SavingsAccount    CurrentAccount
             |                 |
             -------------------
                      |
               BankAccount
Enter fullscreen mode Exit fullscreen mode

তবে এখানে Account-কেই মূল abstract parent class হিসেবে রাখবে; আলাদা BankAccount parent বানানোর প্রয়োজন নেই।

অন্যদিকে:

Customer
   |
   └── Customer Information

Transaction
   |
   └── Transaction Information

Bank
   |
   ├── Customers
   ├── Accounts
   └── Transactions
Enter fullscreen mode Exit fullscreen mode

3. তোমার Project-এ আর কোন OOP Concepts আছে?

চারটি core pillar ছাড়াও তুমি আরও কিছু গুরুত্বপূর্ণ OOP concepts ব্যবহার করবে।

Concept কোথায় ব্যবহার করবে
Class & Object Customer, Account, Bank
Constructor init()
Instance Attributes Customer name, account balance
Instance Methods deposit(), withdraw()
Class Attributes Account type বা shared configuration
Inheritance SavingsAccount, CurrentAccount
Encapsulation Private balance
Polymorphism Different withdrawal behaviours
Abstraction ABC, abstract methods
Method Overriding Child class-এর নিজস্ব methods
super() Parent constructor/method access
@property Controlled attribute access
@classmethod Class-level operations
@staticmethod Utility methods

4. আমার Recommendation

তোমার শেখার সুবিধার জন্য এই ক্রমে OOP concepts implement করো:

  • Class & Object

  • Constructor (__init__)

  • Instance attributes and methods

  • Encapsulation

  • Inheritance

  • Method Overriding

  • Polymorphism

  • Abstraction

  • super(), @property, @classmethod, @staticmethod

একটা গুরুত্বপূর্ণ বিষয়: তোমার বর্তমান Project Requirements-এ চারটি OOP pillars-ই রয়েছে। তাই নতুন করে OOP stage যোগ করার প্রয়োজন নেই। Stage 04 এবং Stage 05 যথেষ্ট।

Top comments (0)