> For the complete documentation index, see [llms.txt](https://mominur.gitbook.io/mominur-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mominur.gitbook.io/mominur-notes/system-design/design-principles/abstraction.md).

# Abstraction

আপনি কি কখনো ভেবেছেন, আমরা যখন একটি গাড়ি চালাই, তখন কিভাবে brake চাপলেই গাড়ি থেমে যায়? কিন্তু brake system এর ভেতরের জটিল hydraulic system বা mechanics আমাদের জানার দরকার হয় না। আমরা শুধু **brake চাপবো** এই concept জানি, কিন্তু এর ভেতরের সব implementation details আমাদের চোখের আড়ালে থাকে। এই ধারণাটাকেই Software Engineering-এ বলা হয় **Abstraction**।

***

### Abstraction কী?

**Abstraction** হলো একটি **Design Principle** যেটা আমাদেরকে একটি জটিল সিস্টেম থেকে অপ্রয়োজনীয় details লুকিয়ে রেখে শুধুমাত্র **necessary information** ইউজার বা Developer-এর কাছে উপস্থাপন করতে সাহায্য করে।

* এটি মূলত **What to do** এবং **How to do** এর মধ্যে পার্থক্য তৈরি করে।
* অর্থাৎ, **Interface** এর মাধ্যমে আমরা জানাতে পারি "কোন কাজ করা হবে", কিন্তু তার ভেতরের **Implementation** লুকিয়ে রাখা হয়।

এক কথায়: **Abstraction** মানে হলো Hide the complexity, show the essentials

***

### কেন Abstraction দরকার?

1. **Code Reusability:** আলাদা Implementation রেখে একই Interface বারবার ব্যবহার করা যায়।
2. **Maintainability:** System এর জটিলতা কমে যায়, ফলে কোড Maintain করা সহজ হয়।
3. **Security:** ইউজার বা অন্য মডিউল unnecessary internal details দেখতে পায় না।
4. **Scalability:** বড় সিস্টেমেও সহজে নতুন Implementation যোগ করা যায়।

***

### Example

#### উদাহরণ ১: Abstract Class ব্যবহার

{% code lineNumbers="true" %}

```php
<?php
abstract class Shape {
    // Abstract method (implementation নেই)
    abstract public function area();
    
    // Normal method (implementation আছে)
    public function description() {
        return "This is a shape.";
    }
}

class Circle extends Shape {
    private $radius;

    public function __construct($radius) {
        $this->radius = $radius;
    }

    // Abstract method এর Implementation
    public function area() {
        return pi() * pow($this->radius, 2);
    }
}

class Rectangle extends Shape {
    private $length;
    private $width;

    public function __construct($length, $width) {
        $this->length = $length;
        $this->width  = $width;
    }

    // Abstract method এর Implementation
    public function area() {
        return $this->length * $this->width;
    }
}

// ব্যবহার
$circle = new Circle(5);
echo "Circle Area: " . $circle->area() . "";

$rectangle = new Rectangle(4, 6);
echo "Rectangle Area: " . $rectangle->area();
?>
```

{% endcode %}

এখানে `Shape` হলো Abstract Class, যেটা শুধু বলে দিচ্ছে প্রতিটি Shape এর একটি `area()` থাকবে। কিন্তু **কিভাবে area হিসাব হবে সেটা আলাদা আলাদা Class এ Implement করা হয়েছে।**

***

#### উদাহরণ ২: Interface ব্যবহার

{% code lineNumbers="true" %}

```php
<?php
interface PaymentGateway {
    public function pay($amount);
}

class PayPal implements PaymentGateway {
    public function pay($amount) {
        echo "Paid $amount using PayPal";
    }
}

class Stripe implements PaymentGateway {
    public function pay($amount) {
        echo "Paid $amount using Stripe";
    }
}

// ব্যবহার
function processPayment(PaymentGateway $gateway, $amount) {
    $gateway->pay($amount);
}

processPayment(new PayPal(), 100);
processPayment(new Stripe(), 200);
?>
```

{% endcode %}

এখানে `PaymentGateway` Interface শুধু declare করেছে "pay() method থাকবে", কিন্তু কিভাবে Payment হবে সেটা প্রতিটি Class (PayPal/Stripe) আলাদাভাবে Implement করেছে।
