File size: 3,693 Bytes
cad5b7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import java.util.ArrayList;
import java.util.List;

// Functional Cohesion Example
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;
    }
}

// Sequential Cohesion Example
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;
    }
}

// Communicational Cohesion Example
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());
    }
}

// Helper classes for the examples
class Order {
    private boolean isValid;
    private double total;
    private boolean isSubmitted;

    public void setValid(boolean valid) { this.isValid = valid; }
    public void setTotal(double total) { this.total = total; }
    public void setSubmitted(boolean submitted) { this.isSubmitted = submitted; }
}

class User {
    private String name;
    private String email;

    public User(String name, String email) {
        this.name = name;
        this.email = email;
    }

    public String getName() { return name; }
    public String getEmail() { return email; }
}

public class CohesionExamples {
    public static void main(String[] args) {
        // Functional Cohesion
        UserAuthentication auth = new UserAuthentication("user1", "password123");
        System.out.println("Password hashed: " + auth.hashPassword());
        System.out.println("Password strength: " + auth.checkPasswordStrength());
        System.out.println("User authenticated: " + auth.authenticate());

        // Sequential Cohesion
        OrderProcessor processor = new OrderProcessor();
        Order order = new Order();
        Order processedOrder = processor.processOrder(order);
        System.out.println("Order processed successfully.");

        // Communicational Cohesion
        User user = new User("John Doe", "[email protected]");
        UserManager userManager = new UserManager(user);
        System.out.println("User valid: " + userManager.validateUser());
        userManager.saveUser();
        userManager.sendWelcomeEmail();

        System.out.println("Cohesion examples created successfully.");
    }
}