> 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/soc-design-principle.md).

# SoC Design Principle

Software development-এ যখন project বড় হয়, তখন কোড অনেক জায়গায় intertwined হয়ে যায়। অর্থাৎ, একটাই function বা class অনেক কাজ করে – data process করা, user interface handle করা, database interaction করা ইত্যাদি। এটি maintenance, debugging এবং scalability কে কঠিন করে তোলে।

**Separation of Concerns (SoC)** principle এই সমস্যার সমাধান দেয়।

***

### SoC Principle কী?

**Separation of Concerns (SoC)** হলো একটি design principle guideline যা বলে:

> একই code বা module multiple responsibilities handle করবে না। প্রতিটি module বা class একটাই concern বা responsibility handle করবে।

ধরে নাও একটি factory-তে বিভিন্ন worker আছে। একজন worker শুধু assemble করে, অন্যজন quality check করে, আর একজন packaging করে।

* যদি সব worker সব কাজ করতে চায়, chaos হবে।
* যদি প্রতিজন শুধু তার responsibility handle করে, efficiency বাড়ে।

ঠিক সেইভাবে, software-এও প্রতিটি module বা function কে একটি single responsibility দেওয়া উচিত।

***

### Bad Example (SoC না মানা)

{% code lineNumbers="true" %}

```php
class User {
    public $name;
    public $email;

    // Database logic mixed with business logic
    public function save() {
        $conn = new mysqli("localhost", "root", "", "test");
        $sql = "INSERT INTO users (name, email) VALUES ('$this->name', '$this->email')";
        $conn->query($sql);
        $conn->close();
    }

    // UI logic mixed here
    public function display() {
        echo "User: $this->name, Email: $this->email";
    }
}
```

{% endcode %}

**সমস্যা:**

* এক class multiple concerns handle করছে: business logic, database access, এবং UI rendering।
* Code hard to maintain এবং scalable নয়।

***

### Good Example (SoC মানা)

{% code lineNumbers="true" %}

```php
// Entity class (Business logic only)
class User {
    public $name;
    public $email;
}

// Repository class (Database access)
class UserRepository {
    public function save(User $user) {
        $conn = new mysqli("localhost", "root", "", "test");
        $sql = "INSERT INTO users (name, email) VALUES ('$user->name', '$user->email')";
        $conn->query($sql);
        $conn->close();
    }
}

// UI class (Presentation)
class UserView {
    public function display(User $user) {
        echo "User: $user->name, Email: $user->email";
    }
}
```

{% endcode %}

**Benefits:**

* প্রতিটি class একটি single concern handle করছে।
* Easy to maintain, test, and extend।
* Future changes সহজ: যেমন database পরিবর্তন করলে UserRepository পরিবর্তন করলেই হবে, UserView বা User class touch করতে হবে না।

***

### SoC Principle এর Benefits

1. Maintainability: Code পরিবর্তন সহজ এবং predictable।
2. Testability: প্রতিটি concern আলাদাভাবে test করা যায়।
3. Reusability: Module অন্য project বা context-এ reuse করা সহজ।
4. Scalability: Project বড় হলেও structure organized থাকে।

***

**Separation of Concerns (SoC)** principle মানলে কোড clean, organized, এবং modular হয়।
