Architectural Patterns
MVC (Model-View-Controller)
MVC separates an application into three interconnected components:
- Model: Manages data and business logic.
- View: Represents UI and presentation layer.
- Controller: Handles user input and updates the Model and View.
Benefits
- Separation of concerns
- Easier to test and maintain
- Promotes modularity
Example
# Model
class User:
def __init__(self, username):
self.username = username
# View
class UserView:
def show_user(self, user):
print(f"User: {user.username}")
# Controller
class UserController:
def __init__(self, user, view):
self.user = user
self.view = view
def update_username(self, new_name):
self.user.username = new_name
def display_user(self):
self.view.show_user(self.user)
# Usage
user = User("abhishek")
view = UserView()
controller = UserController(user, view)
controller.display_user()
controller.update_username("abhi_updated")
controller.display_user()
Layered Architecture (N-tier)
Divides the application into layers:
- Presentation Layer: UI
- Business Logic Layer: Core functionalities
- Data Access Layer: Interacts with DB