ozayezerceli commited on
Commit
cad5b7a
1 Parent(s): 4742562

Upload 3 files

Browse files
CohesionExamples.java ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import java.util.ArrayList;
2
+ import java.util.List;
3
+
4
+ // Functional Cohesion Example
5
+ class UserAuthentication {
6
+ private String username;
7
+ private String password;
8
+
9
+ public UserAuthentication(String username, String password) {
10
+ this.username = username;
11
+ this.password = password;
12
+ }
13
+
14
+ public String hashPassword() {
15
+ // Hash the password
16
+ return "hashed_" + password;
17
+ }
18
+
19
+ public boolean checkPasswordStrength() {
20
+ // Check if the password meets security requirements
21
+ return password.length() >= 8;
22
+ }
23
+
24
+ public boolean authenticate() {
25
+ // Authenticate the user
26
+ return username != null && password != null;
27
+ }
28
+ }
29
+
30
+ // Sequential Cohesion Example
31
+ class OrderProcessor {
32
+ public Order processOrder(Order order) {
33
+ Order validatedOrder = validateOrder(order);
34
+ Order calculatedOrder = calculateTotal(validatedOrder);
35
+ return submitOrder(calculatedOrder);
36
+ }
37
+
38
+ private Order validateOrder(Order order) {
39
+ // Validate the order
40
+ order.setValid(true);
41
+ return order;
42
+ }
43
+
44
+ private Order calculateTotal(Order order) {
45
+ // Calculate the total price
46
+ order.setTotal(100.0); // Dummy calculation
47
+ return order;
48
+ }
49
+
50
+ private Order submitOrder(Order order) {
51
+ // Submit the order to the system
52
+ order.setSubmitted(true);
53
+ return order;
54
+ }
55
+ }
56
+
57
+ // Communicational Cohesion Example
58
+ class UserManager {
59
+ private User userData;
60
+
61
+ public UserManager(User userData) {
62
+ this.userData = userData;
63
+ }
64
+
65
+ public boolean validateUser() {
66
+ // Validate user data
67
+ return userData.getName() != null && userData.getEmail() != null;
68
+ }
69
+
70
+ public void saveUser() {
71
+ // Save user to database
72
+ System.out.println("User saved: " + userData.getName());
73
+ }
74
+
75
+ public void sendWelcomeEmail() {
76
+ // Send welcome email to user
77
+ System.out.println("Welcome email sent to: " + userData.getEmail());
78
+ }
79
+ }
80
+
81
+ // Helper classes for the examples
82
+ class Order {
83
+ private boolean isValid;
84
+ private double total;
85
+ private boolean isSubmitted;
86
+
87
+ public void setValid(boolean valid) { this.isValid = valid; }
88
+ public void setTotal(double total) { this.total = total; }
89
+ public void setSubmitted(boolean submitted) { this.isSubmitted = submitted; }
90
+ }
91
+
92
+ class User {
93
+ private String name;
94
+ private String email;
95
+
96
+ public User(String name, String email) {
97
+ this.name = name;
98
+ this.email = email;
99
+ }
100
+
101
+ public String getName() { return name; }
102
+ public String getEmail() { return email; }
103
+ }
104
+
105
+ public class CohesionExamples {
106
+ public static void main(String[] args) {
107
+ // Functional Cohesion
108
+ UserAuthentication auth = new UserAuthentication("user1", "password123");
109
+ System.out.println("Password hashed: " + auth.hashPassword());
110
+ System.out.println("Password strength: " + auth.checkPasswordStrength());
111
+ System.out.println("User authenticated: " + auth.authenticate());
112
+
113
+ // Sequential Cohesion
114
+ OrderProcessor processor = new OrderProcessor();
115
+ Order order = new Order();
116
+ Order processedOrder = processor.processOrder(order);
117
+ System.out.println("Order processed successfully.");
118
+
119
+ // Communicational Cohesion
120
+ User user = new User("John Doe", "[email protected]");
121
+ UserManager userManager = new UserManager(user);
122
+ System.out.println("User valid: " + userManager.validateUser());
123
+ userManager.saveUser();
124
+ userManager.sendWelcomeEmail();
125
+
126
+ System.out.println("Cohesion examples created successfully.");
127
+ }
128
+ }
CouplingExamples.java ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Tight Coupling Example
2
+ class Database {
3
+ public User getUser(int userId) {
4
+ // database logic here
5
+ return new User(userId);
6
+ }
7
+ }
8
+
9
+ class UserService {
10
+ private Database db;
11
+
12
+ public UserService() {
13
+ this.db = new Database();
14
+ }
15
+
16
+ public User getUserInfo(int userId) {
17
+ return db.getUser(userId);
18
+ }
19
+ }
20
+
21
+ // Loose Coupling Example
22
+ class LooselyCoupledUserService {
23
+ private Database db;
24
+
25
+ public LooselyCoupledUserService(Database database) {
26
+ this.db = database;
27
+ }
28
+
29
+ public User getUserInfo(int userId) {
30
+ return db.getUser(userId);
31
+ }
32
+ }
33
+
34
+ // Decoupling with Interfaces Example
35
+ interface DatabaseInterface {
36
+ User getUser(int userId);
37
+ }
38
+
39
+ class DatabaseImpl implements DatabaseInterface {
40
+ public User getUser(int userId) {
41
+ // database logic here
42
+ return new User(userId);
43
+ }
44
+ }
45
+
46
+ class DecoupledUserService {
47
+ private DatabaseInterface db;
48
+
49
+ public DecoupledUserService(DatabaseInterface database) {
50
+ this.db = database;
51
+ }
52
+
53
+ public User getUserInfo(int userId) {
54
+ return db.getUser(userId);
55
+ }
56
+ }
57
+
58
+ // Helper class for the examples
59
+ class User {
60
+ private int id;
61
+
62
+ public User(int id) {
63
+ this.id = id;
64
+ }
65
+ }
66
+
67
+ public class CouplingExamples {
68
+ public static void main(String[] args) {
69
+ // Tight Coupling
70
+ UserService tightlyCoupleddService = new UserService();
71
+ User user1 = tightlyCoupleddService.getUserInfo(1);
72
+
73
+ // Loose Coupling
74
+ Database database = new Database();
75
+ LooselyCoupledUserService looselyCoupleddService = new LooselyCoupledUserService(database);
76
+ User user2 = looselyCoupleddService.getUserInfo(2);
77
+
78
+ // Decoupling with Interfaces
79
+ DatabaseInterface databaseInterface = new DatabaseImpl();
80
+ DecoupledUserService decoupledService = new DecoupledUserService(databaseInterface);
81
+ User user3 = decoupledService.getUserInfo(3);
82
+
83
+ System.out.println("Coupling examples created successfully.");
84
+ }
85
+ }
MonolithicEcommerceApp.java ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import java.util.*;
2
+
3
+ class EcommerceApp {
4
+ private Map<String, User> users = new HashMap<>();
5
+ private Map<Integer, Product> products = new HashMap<>();
6
+ private Map<Integer, Order> orders = new HashMap<>();
7
+ private Map<Integer, Payment> payments = new HashMap<>();
8
+ private Map<String, List<String>> userPreferences = new HashMap<>();
9
+
10
+ public void registerUser(String username, String password) {
11
+ users.put(username, new User(username, password));
12
+ userPreferences.put(username, new ArrayList<>());
13
+ }
14
+
15
+ public void addProduct(int productId, String name, double price, String category) {
16
+ products.put(productId, new Product(productId, name, price, category));
17
+ }
18
+
19
+ public Integer createOrder(String user, int productId, int quantity) {
20
+ if (users.containsKey(user) && products.containsKey(productId)) {
21
+ int orderId = orders.size() + 1;
22
+ orders.put(orderId, new Order(orderId, user, productId, quantity));
23
+ userPreferences.get(user).add(products.get(productId).getCategory());
24
+ return orderId;
25
+ }
26
+ return null;
27
+ }
28
+
29
+ public boolean processPayment(int orderId, String paymentMethod) {
30
+ if (orders.containsKey(orderId)) {
31
+ payments.put(orderId, new Payment(paymentMethod, "completed"));
32
+ return true;
33
+ }
34
+ return false;
35
+ }
36
+
37
+ public List<Product> recommendProducts(String user, int numRecommendations) {
38
+ if (!userPreferences.containsKey(user) || userPreferences.get(user).isEmpty()) {
39
+ return new ArrayList<>(products.values()).subList(0, Math.min(numRecommendations, products.size()));
40
+ }
41
+
42
+ List<String> userCategories = userPreferences.get(user);
43
+ String mostCommonCategory = getMostCommonCategory(userCategories);
44
+
45
+ List<Product> recommendedProducts = products.values().stream()
46
+ .filter(p -> p.getCategory().equals(mostCommonCategory))
47
+ .limit(numRecommendations)
48
+ .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
49
+
50
+ if (recommendedProducts.size() < numRecommendations) {
51
+ List<Product> otherProducts = products.values().stream()
52
+ .filter(p -> !p.getCategory().equals(mostCommonCategory))
53
+ .limit(numRecommendations - recommendedProducts.size())
54
+ .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
55
+ recommendedProducts.addAll(otherProducts);
56
+ }
57
+
58
+ return recommendedProducts;
59
+ }
60
+
61
+ private String getMostCommonCategory(List<String> categories) {
62
+ return categories.stream()
63
+ .collect(java.util.stream.Collectors.groupingBy(c -> c, java.util.stream.Collectors.counting()))
64
+ .entrySet().stream()
65
+ .max(Map.Entry.comparingByValue())
66
+ .map(Map.Entry::getKey)
67
+ .orElse(null);
68
+ }
69
+ }
70
+
71
+ class User {
72
+ private String username;
73
+ private String password;
74
+
75
+ public User(String username, String password) {
76
+ this.username = username;
77
+ this.password = password;
78
+ }
79
+ }
80
+
81
+ class Product {
82
+ private int id;
83
+ private String name;
84
+ private double price;
85
+ private String category;
86
+
87
+ public Product(int id, String name, double price, String category) {
88
+ this.id = id;
89
+ this.name = name;
90
+ this.price = price;
91
+ this.category = category;
92
+ }
93
+
94
+ public String getCategory() {
95
+ return category;
96
+ }
97
+
98
+ @Override
99
+ public String toString() {
100
+ return name + " ($" + price + ")";
101
+ }
102
+ }
103
+
104
+ class Order {
105
+ private int id;
106
+ private String user;
107
+ private int productId;
108
+ private int quantity;
109
+
110
+ public Order(int id, String user, int productId, int quantity) {
111
+ this.id = id;
112
+ this.user = user;
113
+ this.productId = productId;
114
+ this.quantity = quantity;
115
+ }
116
+ }
117
+
118
+ class Payment {
119
+ private String method;
120
+ private String status;
121
+
122
+ public Payment(String method, String status) {
123
+ this.method = method;
124
+ this.status = status;
125
+ }
126
+ }
127
+
128
+ public class MonolithicEcommerceApp {
129
+ public static void main(String[] args) {
130
+ EcommerceApp app = new EcommerceApp();
131
+
132
+ app.registerUser("alice", "password123");
133
+ app.addProduct(1, "Laptop", 999.99, "Electronics");
134
+ app.addProduct(2, "Smartphone", 499.99, "Electronics");
135
+ app.addProduct(3, "Headphones", 99.99, "Electronics");
136
+ app.addProduct(4, "T-shirt", 19.99, "Clothing");
137
+
138
+ Integer orderId = app.createOrder("alice", 1, 1);
139
+ if (orderId != null) {
140
+ app.processPayment(orderId, "credit_card");
141
+ }
142
+
143
+ List<Product> recommendations = app.recommendProducts("alice", 2);
144
+ System.out.println("Recommended products for Alice:");
145
+ for (Product product : recommendations) {
146
+ System.out.println("- " + product);
147
+ }
148
+
149
+ System.out.println("Monolithic architecture example created successfully.");
150
+ }
151
+ }