File size: 5,321 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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
import streamlit as st
from utils.code_examples import cohesion_examples
from utils.quiz import cohesion_quiz
st.title("Cohesion in Software Architecture")
st.write("""
Cohesion refers to the degree to which the elements within a module belong together.
High cohesion is generally associated with good software design.
""")
st.subheader("Types of Cohesion")
st.write("""
1. **Functional Cohesion**: All elements of the module are related to the performance of a single function.
2. **Sequential Cohesion**: Elements of a module are involved in activities where output from one element serves as input to another.
3. **Communicational Cohesion**: Elements of a module operate on the same data.
4. **Procedural Cohesion**: Elements of a module are involved in different and possibly unrelated actions that must be performed in a specific order.
5. **Temporal Cohesion**: Elements are related by the fact that they are processed at a particular time in program execution.
6. **Logical Cohesion**: Elements perform similar operations, but these operations are unrelated.
7. **Coincidental Cohesion**: Elements have no meaningful relationship to one another.
""")
st.subheader("Examples of Cohesion")
for i, (title, description, python_code) in enumerate(cohesion_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 == "Functional Cohesion":
java_code = """
class UserAuthentication {
private String username;
private String password;
public UserAuthentication(String username, String password) {
this.username = username;
this.password = password;
}
public String hashPassword() {
// Hash the password
return "hashed_" + password;
}
public boolean checkPasswordStrength() {
// Check if the password meets security requirements
return password.length() >= 8;
}
public boolean authenticate() {
// Authenticate the user
return username != null && password != null;
}
}
"""
elif title == "Sequential Cohesion":
java_code = """
class OrderProcessor {
public Order processOrder(Order order) {
Order validatedOrder = validateOrder(order);
Order calculatedOrder = calculateTotal(validatedOrder);
return submitOrder(calculatedOrder);
}
private Order validateOrder(Order order) {
// Validate the order
order.setValid(true);
return order;
}
private Order calculateTotal(Order order) {
// Calculate the total price
order.setTotal(100.0); // Dummy calculation
return order;
}
private Order submitOrder(Order order) {
// Submit the order to the system
order.setSubmitted(true);
return order;
}
}
"""
elif title == "Communicational Cohesion":
java_code = """
class UserManager {
private User userData;
public UserManager(User userData) {
this.userData = userData;
}
public boolean validateUser() {
// Validate user data
return userData.getName() != null && userData.getEmail() != null;
}
public void saveUser() {
// Save user to database
System.out.println("User saved: " + userData.getName());
}
public void sendWelcomeEmail() {
// Send welcome email to user
System.out.println("Welcome email sent to: " + userData.getEmail());
}
}
"""
st.code(java_code, language="java")
st.subheader("Interactive Exercise: Identify Cohesion Types")
st.write("Analyze the following code snippets and identify the type of cohesion present.")
code1 = """
class UserManager:
def create_user(self, username, email):
# logic to create user
pass
def delete_user(self, user_id):
# logic to delete user
pass
def update_user(self, user_id, new_data):
# logic to update user
pass
"""
code2 = """
class ReportGenerator:
def generate_sales_report(self):
# logic for sales report
pass
def generate_inventory_report(self):
# logic for inventory report
pass
def send_email(self, report):
# logic to send email
pass
"""
st.code(code1, language="python")
user_answer1 = st.selectbox("What type of cohesion is present in the first example?",
("Functional", "Sequential", "Communicational", "Procedural", "Temporal", "Logical", "Coincidental"))
st.code(code2, language="python")
user_answer2 = st.selectbox("What type of cohesion is present in the second example?",
("Functional", "Sequential", "Communicational", "Procedural", "Temporal", "Logical", "Coincidental"))
if st.button("Check Answers"):
if user_answer1 == "Functional" and user_answer2 == "Logical":
st.success("Correct! The UserManager class shows functional cohesion, while the ReportGenerator class shows logical cohesion.")
else:
st.error("Not quite. Try again!")
st.markdown("---")
st.subheader("Quiz: Cohesion Concepts")
cohesion_quiz()
|