Junior — Middle
What is the purpose and advantages of using the MVC architectural pattern?
sobes.tech AI
Answer from AI
The architectural pattern MVC (Model-View-Controller) divides an application into three components:
- Model — responsible for data and business logic.
- View — displays data to the user.
- Controller — handles user input and interacts with the model and view.
Advantages of using MVC:
- Separation of concerns makes code maintenance and development easier.
- Increased code reusability — the model and view can be used independently.
- Simplified testing — logic can be tested separately from the interface.
- Improved code organization — a clear structure helps teams work more efficiently.
Example in Python (simplified):
class Model:
def __init__(self):
self.data = "Hello MVC"
class View:
def display(self, data):
print(f"View: {data}")
class Controller:
def __init__(self, model, view):
self.model = model
self.view = view
def update_view(self):
self.view.display(self.model.data)
model = Model()
view = View()
controller = Controller(model, view)
controller.update_view()