Most of the programmers would respond saying...Encapsulation is about making variables private and exposing them using public properties or methods i.e. you have put the variables in a capsule !!!
This is indeed true but there is something more to encapsulation. If we go by this definition we have capsuled the properties.
The things that can be encapsulated are
1. Private Data
2. Public properties !!!
3. Behavior !!!
2 and 3 would be something new to people familiar with the classic definition of encapsulation. So encapsulation is not just about keeping data in your classes separate from rest of your application it is also about grouping your application into logical parts i.e. protect information in one part of your application from other parts. It is also known as information hiding (and now you know this information is not just data) or separation of concerns.
Why to encapsulate ?
When you break the behavior or properties out from a class, you can change the behavior without changing the class. So by breaking up the different parts of your application, you can change one part without having to change all the other parts. So encapsulation makes your code easier to change and leads to a flexible software.
When to encapsulate ?
1. Anytime you see duplicate code, look for a place to encapsulate.
2. Encapsulate what varies.
How to encapsulate ?
Take the portions that you want to move out from a class and move them into a new class and you have achieved encapsulation. So one way of encapsulating is by creating classes.
Now lets talk about encapsulating properties and behavior.
Encapsulating properties (group of properties)
Lets assume you have the following two classes
1. Book
-Name
-Author
-SerialNumber
-Price
2. BookStore
-Book[] Search(Book)
-AddBook(Book)
The Search method takes Book parameter for searching the books. The Book parameter is used to hold the search specification by user i.e Name and Author. The user can't specify SerialNumber and Price and hence they would be null when passed to Search.
Something is wrong !!! The Book object does not seem to represent a single concept, it is representing both the Book entity and search specification. So time to create a new a new class BookSpec which has Name and Author as properties.
1. Book
-Name
-Author
-SerialNumber
-Price
2. BookStore
-Book[] Search(BookSpec)
-AddBook(Book)
3. BookSpec
-Name
-Author
Still something that can be impoved...duplicate code. The Name and Author are same in both BookSpec and Book. So lets move Name and Author out of Book and instead let it refer BookSpec.
1. Book
-BookSpec
-SerialNumber
-Price
2. BookStore
-Book[] Search(BookSpec)
-AddBook(Book)
3. BookSpec
-Name
-Author
So you have encapsulated a group of properties from Book class into BookSpec class. So would you still say encapsulation is just about private properties :)
Infact, we have also touched upon an interesting pattern called the Specification pattern which we would be discussing in a separate post. You can refer to Martin Fowler's article but it would need a lot of patience ;)
Encapsulating behavior
Say we have a Car class..
Car
-Start()
-Stop()
-Drive()
Anything wrong !!! Do you think that car drives itself ? Isn't it the driver who would driver the Car ?
So lets create two classes
Car
-Start()
-Stop()
Driver
-Drive(Car)
Though not a very good example, still I think we have seen how behavior can be encapsulated in a new class.
Let me know if the post was helpful !!!
Why did the title say that encapsulation is the big chunk of the PIE (Polymorphism, Inheritence and Encapsulation). Don't you think that Inheritence and Polymorphism are specialised forms of encapsulation ? Ah....forget it if it confused you.
Saturday, June 14, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment