T B H P N

Design Concept - Encapsulate Classes and Packages

Classes should provide a simple public interface that does not allow binding to its implementation. That is, client code should only be able to access the class's public interface member functions and should not be able to directly access its member data and private member functions. We say that such classes are encapsulated.

The implementation of a class is subject to change every time we fix latent errors or add functionality to the class. That will break the design of any client that binds to its implementation, causing a cascade of consequential changes. The class's public interface changes much less frequently, so encapsulation is a mechanism to minimize the affects of change.

Exactly the same statements can be made for packages. A package is similar to a class that only supports one instance. Its global functions and global data act like member functions and member data of a class. We can make package global data private by qualifying its declaration with the keyword static. Similarly its global functions are public unless they are also qualified with static. Of course packages can host classes, but classes can also host nested classes, so the analogy holds in this case too.

Conclusions for Encapsulate Classes and Packages:

Encapsule the classes and packages you design:
  1. Make all data private. Try to avoid returning pointers and passing arguments by non-constant reference.
  2. Make all public functions have simple and meaningful signatures; and try to ensure they can be used without understanding the design of the class. This encapsulates "need to know".
  3. C++ classes control access by using the keywords: public, protected, and private.
  4. Packages implemented using C and C++ make global functions and global data private by qualifying with the static keyword. Otherwise they are public.

CST strip