Communicational cohesion occurs when parts of a module are grouped together because they use the same set of inputs and outputs. If a module has different elements that have been grouped together because they access and modify the same data structure, it would demonstrate communicational cohesion. For example, a data structure that represents the contents of a customer’s shopping basket might be used by a variety of elements in a single module. The elements might calculate discounts, shipping, and taxes based on the same data structure. This level of cohesion is moderate and usually considered acceptable 1.
To remove communicational cohesion, you can follow these steps:
- Separate functionality into modules: If a module has multiple functionalities, it can be difficult to modify and maintain. By separating functionalities into different modules, you can reduce the scope of changes required for each modification.
- Use a common data format: If multiple modules need to access the same data, it is important to use a common data format. This can help reduce the complexity of data exchange between modules.
- Use a messaging system: A messaging system can be used to decouple modules that need to communicate with each other. This can help reduce the dependency between modules and improve the overall flexibility of the system.
Here is an example of how to remove communicational cohesion:
- 기능을 모듈로 분리: 모듈에 여러 기능이 있는 경우 유지보수성이 떨어진다. 기능을 여러 모듈로 분리한다.
- 공통 데이터 포맷 사용: 모듈 간 데이터 공유 복잡성을 줄인다.
- 메시징 시스템을 사용 : 모듈 간의 종속성을 줄이고 시스템의 전반적인 유연성을 향상시키는 데 도움이 될 수 있다.
통신적 결합도 제거 코드
장바구니 배열 자체를 객체로 변경하면 된다.
위 문단에서 설명한 방법 중 1, 2를 적용한다.
public class ShoppingCart {
private List<Item> items;
public ShoppingCart() {
items = new ArrayList<>();
}
public void addItem(Item item) {
items.add(item);
}
public void removeItem(Item item) {
items.remove(item);
}
public List<Item> getItems() {
return items;
}
}
public class DiscountCalculator {
public double calculateDiscount(ShoppingCart cart) {
// Calculate discount based on cart items
}
}
public class ShippingCalculator {
public double calculateShipping(ShoppingCart cart) {
// Calculate shipping cost based on cart items
}
}
public class TaxCalculator {
public double calculateTax(ShoppingCart cart) {
// Calculate tax based on cart items
}
}
'BackEnd' 카테고리의 다른 글
[SW공학] 절차적 응집도와 해결방안 (0) | 2023.12.10 |
---|---|
[Java] eclipse(이클립스) marketplace(마켓플레이스) 다운로드 SSL 우회하는 법 (0) | 2023.11.27 |
[Java, Spring] 리플렉션을 활용한 프록시 동적 생성 (0) | 2023.06.19 |
[Java, Spring] 프록시, 프록시 패턴, 데코레이터 패턴 (0) | 2023.06.18 |
[Java, Spring] 쓰레드 로컬과 전략 패턴, 탬플릿 메서드 패턴 (0) | 2023.06.04 |