ConstructiCat Logo
CodeBust.
Browse section ▾

Inappropriate Intimacy.

One class uses the internal fields and methods of another class.

##Signs and Symptoms

One class uses the internal fields and methods of another class.

class Customer {
  List<Order> orders = new ArrayList<>();
  void cancelOrders() {
    for (Order order : orders) order.status = "CANCELLED"; // writes Order's field directly
  }
}
class Order {
  String status = "NEW";
  Customer customer;
  void assignTo(Customer customer) {
    this.customer = customer;
    customer.orders.add(this); // edits Customer's internal list
  }
}

##Reasons for the Problem

Keep a close eye on classes that spend too much time together. Good classes should know as little about each other as possible. Such classes are easier to maintain and reuse.

##Treatment

##Payoff

  • Improves code organization.

  • Simplifies support and code reuse.