Home > Patterns and Practices > Encapsulation fundamentals

Encapsulation fundamentals

Fowler writes about getter setter methods (properties) http://martinfowler.com/bliki/GetterEradicator.html. He links to some great articles on encapsulation, open closed principle, protected variation, tell don’t ask etc.
 
To quote some snippets worth remembering from Craig Larman’s article
 
"However, the term has been so widely reinterpreted as a synonym for data encapsulation that it is no longer possible to use it in its original sense without misunderstanding it."
 
in short Information Hiding != Data Encapsulation.
 
"the Color static fields red, black, white, and so forth, are extremely stable; the likelihood of instability is so low that making them private and adding accessing methods is just object purism."
 
This reminds me of something which i read in an Uncle Bob’s article on OCP. "Strategic Closure".
 
To conclude, Martin Fowlers words
 
"Allocation of behavior between objects is the essence of object-oriented design, so like any design, there isn’t a hard and fast rule – rather a judging of trade-offs. Putting the behavior in the same class as the data, what Craig Larman calls "Information Expert", is the first choice to consider. But it isn’t the only route. Layering often trumps this, many of the Gang of Four patterns separate data from behavior for particular needs. A good rule of thumb is that things that change together should be together. Data and the behavior that uses it often change together, but often you see other groupings that matter more. "
 
says it all.
 
 
So are you a purist or a pragmatist?
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: