|
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) |
|
|
|
|
|
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() |
|
|