What is the difference between Encapsulation and High cohesion?

I’ve looked at encapsulation and cohesion, but they look like the same thing to me. Isn’t an encapsulated object already highly-cohesive?

1

Cohesion refers to the degree that elements of a module belong together. The more cohesive a module is, relative to other modules in the software, the more independently it can operate. Encapsulation is one of the techniques by which cohesion can be achieved. While encapsulation is certainly an important element of cohesion, cohesion also addresses other concerns that affect cohesion.

The degree in which components depend on each other for their proper operation is called coupling. Coupling is achieved primarily by the mechanisms chosen to interact between components. The less coupled a system is, the more cohesive it is. Hence, low coupling is a desirable feature.

Let’s explore a few examples of relationships between components, in order of increasing cohesivity:

Inheritance
Inheritance is very tightly-coupled, and therefore has relatively low cohesivity; if you break the inheritance, you break the program. But inheritance is desirable for systems where you want and need that kind of tight coupling, because you want the components to work closely together. It is used quite effectively in Graphical User Interfaces, for example.

Composition
Composition is less tightly-coupled than Inheritance; you can replace a local object in a class with a different implementation without breaking an inheritance chain. But you still have to change the class’s internal implementation; it’s just that the changes are confined to a single class.

Composition with interfaces
Here, the class is only dependent on an Interface. Consequently, you can change out the implementation without changing the class at all, provided the implementing class conforms to the specified interface. You can even change out the implementation of the interface at runtime, by handing an instance of the desired class to the constructor of your class.

Data Transfer Objects and View Models
This technique allows you to create classes that serve as data containers, without any behavior associated with them. Now, the modules between which you are communicating don’t need to know anything about each other at all, not even a common interface. All they need to know about is the DTO or View Model object. But things are beginning to become a bit more complicated, because now you’re spending a lot of time mapping fields to other fields and copying data between different structures.

Streams, strings and pipes
With strings and streams, you take advantage of a well-known mechanism to communicate between modules. This arrangement is the most loosely-coupled, because the module doesn’t have any dependencies at all, except for widely-available, cross-platform compatible primitive data structures. Strings are the most ubiquitous of all data types; examples of their use in an API include database connection strings and communication protocols that use strings or binary arrays to transmit data. The disadvantage of this mechanism is that you now need a parser, a serializer, or some similar mechanism in your modules to translate the raw data being transmitted between modules into a usable form.

As you can see, high cohesivity isn’t a perfect ideal, nor is it universally desirable. As with many other things in computing, it is a tradeoff between competing objectives.

2

Encapsulation and High Cohesion are different, but related things.

They both deal with class design but focus on different things.

Encapsulation (aka information hiding) is the idea that one shouldn’t expose implementation details – that the class API should not “leak” the internals, making it easier to change said internals later if needed (for instance, exposing an iterator – it may be backed by an array now, but a hash table later on).

High cohesion, on the other end, related to how parts of a class relate to each other, how often they are used together in the class. The more often they are used in conjunction, the higher the cohesiveness of the class.

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