Does shared objects breaks encapsulation?

The encapsulation principle says : “You should hide the attributes of your class and only make them accessible via methods. This guarantees the validity of class invariants.”

I’m totally agree with this principle, but i’ve been wondering : “does sharing the same object between several classes break encapsulation ?”

Example : we have an object of a class A.

  • class B has a reference to this object
  • class C has a reference to this object

It seems ridiculous because the point of OOP is let objects communicate between them, but the thing is a class that has a reference to this shared object can modify without passing through the methods of other classes so logically it breaks the encapsulation ?

For example : B modify the state of the object A without the agreement of C because it doesn’t pass with its methods ? Same for C that doesn’t pass via B.

I’m so confused with all these principles, I feel like they’re all contradicting each other. Do I perhaps misunderstand the principle of encapsulation ?

public class A {
  private int value = 0;

  public void increment(){
    value += 1;
  }

  public void decrement() {
    value -= 1;
  }
}


public class B {
  private final A a;
  
  public B(A a){
    this.a = a;
  }

  public void doSomething(){
    // Do some stuff
    a.increment();
  }
}

public class C {
  private final A a;
  
  public C(A a){
    this.a = a;
  }

  public void doSomethingElse(){
    // Do some stuff
    a.decrement();
  }
}

New contributor

xerox0213 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Yes, sharing mutable state like this is indeed a light break of encapsulation. A few important things to realize:

It’s not inevitable

Lots of classes are immutable. You can’t change anything about them. Trivially, strings, for example. If 2 instances both ‘share’ a string, and one them calls x.toLowerCase(), nothing happens. Because toLowerCase() changes nothing. It makes an entirely new string and returns it. If one class writes x = x.toLowerCase(), they are updating their reference – it does not modify the reference (nor the object) that the other class’s field is referring to.

Sharing immutables therefore does not run into this issue.

The point of architectural debate

Here’s one rule that has no exceptions: “Every rule has exceptions”.

The principle of encapsulation is a guide. And, like all guidelines, true expertise involves knowing when to ignore them.

As a general principle, encapsulation has advantages.

But then, so does sharing state.

The advantages and disadvantages are completely different. So, it’s a tradeoff: Code that you write that breaks encapsulation in certain ways has certain disadvantages. For example, it’s hard to update one class without at least also taking into account you need to consider the impact it has on other classes involved in the ‘break’. But, often, 2 classes are fundamentally related anyway – changing one without keeping in mind the other exists is a problem anyway. At which point, that particular disadvantage is irrelevant.

There are no simple guides. Even if you feel you are really good at reading architectural documentation like this, there’s no real way to fully understand how to apply it without being an experienced programmer.

It’s good news, and bad news: To be a good programmer you need to do it a lot and gain 10 years experience. The downside is: Shortcuts don’t exist. The upside is: Shortcuts don’t exist – so you can just ignore such concerns.

If some architectural design you come up with ends up being a disaster, and if you had put more effort in avoiding encapsulation would have avoided it, [A] that doesn’t mean the alternative would have been any better, and [B] well, you know how beginner programmers turn into expert programmers? Via the tool known as ‘the mistake’. Cherish them. And make sure you keep in mind that post mortems on your coding decisions is crucial. You should spend more time considering the full impact of the choices you have made in the past on how maintainable your software is today, than attempting to read well-intended philosophical gobbledygook about abstract concepts like ‘encapsulation’. I’m not saying things like encapsulation are meaningless; not at all. I’m saying: Without having personal experience, it’s incredibly difficult to meaningfully apply the advice.

Note that my earlier advice about immutables has to adhere to all this just the same. Do not go on a spree of ‘I have seen the light and ALL SHALL BE IMMUTABLE! Time to toss out all our software and start over!’ – that’s making the same mistake. It’s a tool in the toolbox, if you keep running into issues that feel like ‘encapsulation’ is the issue, you might want to try a few features with more immutable architecture. Trust me, you’ll run into a whole new set of disadvantages and maintenance headaches. But, now you learned, and now you can weigh the downsides of foregoing proper encapsulation against the downsides of overindexing on it. Next project you might finally be capable of making a good judgement on which tradeoff is the best for the project you’re working on.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật