File size: 3,710 Bytes
0b0d1e4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
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)
# Create tabs for Python and Java examples
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()
|