|
import streamlit as st |
|
from utils.code_examples import coupling_examples |
|
|
|
st.title("Coupling in Software Architecture") |
|
|
|
st.write(""" |
|
Coupling refers to the degree of interdependence between software modules. |
|
Low coupling is typically a sign of a well-structured computer system and a good design. |
|
""") |
|
|
|
st.subheader("Types of Coupling") |
|
st.write(""" |
|
1. **Tight Coupling**: Modules are highly dependent on each other. |
|
2. **Loose Coupling**: Modules are relatively independent of each other. |
|
""") |
|
|
|
st.subheader("Examples of Coupling") |
|
|
|
for i, (title, description, python_code) in enumerate(coupling_examples, 1): |
|
st.markdown(f"### Example {i}: {title}") |
|
st.write(description) |
|
|
|
|
|
python_tab, java_tab = st.tabs(["Python", "Java"]) |
|
|
|
with python_tab: |
|
st.code(python_code, language="python") |
|
|
|
with java_tab: |
|
if title == "Tight Coupling": |
|
java_code = """ |
|
class Database { |
|
public User getUser(int userId) { |
|
// database logic here |
|
return new User(userId); |
|
} |
|
} |
|
|
|
class UserService { |
|
private Database db; |
|
|
|
public UserService() { |
|
this.db = new Database(); |
|
} |
|
|
|
public User getUserInfo(int userId) { |
|
return db.getUser(userId); |
|
} |
|
} |
|
""" |
|
elif title == "Loose Coupling": |
|
java_code = """ |
|
class Database { |
|
public User getUser(int userId) { |
|
// database logic here |
|
return new User(userId); |
|
} |
|
} |
|
|
|
class UserService { |
|
private Database db; |
|
|
|
public UserService(Database database) { |
|
this.db = database; |
|
} |
|
|
|
public User getUserInfo(int userId) { |
|
return db.getUser(userId); |
|
} |
|
} |
|
""" |
|
elif title == "Decoupling with Interfaces": |
|
java_code = """ |
|
interface DatabaseInterface { |
|
User getUser(int userId); |
|
} |
|
|
|
class DatabaseImpl implements DatabaseInterface { |
|
public User getUser(int userId) { |
|
// database logic here |
|
return new User(userId); |
|
} |
|
} |
|
|
|
class UserService { |
|
private DatabaseInterface db; |
|
|
|
public UserService(DatabaseInterface database) { |
|
this.db = database; |
|
} |
|
|
|
public User getUserInfo(int userId) { |
|
return db.getUser(userId); |
|
} |
|
} |
|
""" |
|
st.code(java_code, language="java") |
|
|
|
st.subheader("Interactive Exercise: Identify Coupling Issues") |
|
st.write("Analyze the following code snippets and identify the type of coupling present.") |
|
|
|
code1 = """ |
|
class Database: |
|
def get_user(self, user_id): |
|
# database logic here |
|
pass |
|
|
|
class UserService: |
|
def __init__(self): |
|
self.db = Database() |
|
|
|
def get_user_info(self, user_id): |
|
return self.db.get_user(user_id) |
|
""" |
|
|
|
code2 = """ |
|
class Database: |
|
def get_user(self, user_id): |
|
# database logic here |
|
pass |
|
|
|
class UserService: |
|
def __init__(self, database): |
|
self.db = database |
|
|
|
def get_user_info(self, user_id): |
|
return self.db.get_user(user_id) |
|
""" |
|
|
|
st.code(code1, language="python") |
|
user_answer1 = st.radio("What type of coupling is present in the first example?", |
|
("Tight Coupling", "Loose Coupling")) |
|
|
|
st.code(code2, language="python") |
|
user_answer2 = st.radio("What type of coupling is present in the second example?", |
|
("Tight Coupling", "Loose Coupling")) |
|
|
|
if st.button("Check Answers"): |
|
if user_answer1 == "Tight Coupling" and user_answer2 == "Loose Coupling": |
|
st.success("Correct! The first example shows tight coupling, while the second shows loose coupling.") |
|
else: |
|
st.error("Not quite. Try again!") |
|
|
|
st.markdown("---") |
|
|
|
st.subheader("Quiz: Coupling Concepts") |
|
from utils.quiz import coupling_quiz |
|
coupling_quiz() |
|
|