Introduction
Today I am introducing a "HelloWorld" a series of small write
up talking about information technology, mainly with emphasis on the world of
software development. There are so much cool stuff available on net talking
about the same, however, I find in comparison majority of target audience is rather
intermediary to advance level. That leaves the beginner level folks with a
prayer, “they will figure it out anyway”. So, here is my take to help them, my
target audience falls in “spoon feed me NOW!” category. For those who don’t belong
to this category, salute goes out to your courage, please continue to read, you
never know!
So, what am I going to talk about? Well, I can talk about OOP
Encapsulation or new HTML5 <canvas> or Multithreading or abracadabra or
anything in and everything in between. Having said “Encapsulation”, why not,
let’s start with this one! Before I begin, I do assume you have some
association with the world of software development, if not, then good luck! Should
you stop reading? Hell no, you are already in the zone, one day, one day it
will help you, trust me, knowledge never goes to waste.
Now, let’s get to the exciting part, if you heard of object
oriented programming (OOP) you are no stranger to “Encapsulation”, however, how
many if you actually know what the heck is Encapsulation anyway? Simply put, it
means “Hide It”. OK. Hide what, and from whom? In the world of OPP, we have
classes, and they are type of domain, in the business world the examples would
be: customer, vendor, inventory, orders and shipping etc. So, if your project
has two classes Inventory and Shipping, there inner details must be hidden from
each other. Let me show you this with the help of the following diagram:
As you can see the diagram, Inventory class has field member
“catalogid”, the implementation of this data field is only visible to the properties
and methods belong to the class, same goes with field member “orderid’ in shipping
class. We usually create interfaces to let classes talk to each other, in this
example, if shipping class needs to know the “catalogid” data, it will send a
request through interface and get the response back through the interface. What
is Interface? Well, slow down, will take a dig at that too soon.
public class Inventory
{
int catalogid;
public int CatalogId
{
set
{
catalogid = value;
}
get
{
return catalogid;
}
}
public int OneMoreCatalogId { get; set; }
}
If you notice the simple C# code implementation of the
Inventory class, you will see the field member “catalogid” of int data type declared
as private. If you create the instance object of this class, you will not able
to refer to this field, because it is encapsulated within the class boundary, that
is what “Hide It!” That’s how it should
be, if you need to access “catalogid” data either for read or write, you will
make use of property CatalogId or “OneMoreCatalogId” both are same, just a different
form of implementation. You can also code methods to manipulate the field. The short
cut method shown in the code is same as the other one, only magic here is the
complier helps you to convert the short cut implementation to regular getter
setter form. The keyword “value” there is nothing but hidden parameter passed
to getter setter.
So encapsulation helps us here to hide the complexity and
inner working of the given class to other classes in the problem domain. In our
example, orders will come to Inventory class as request with the help of
interface and a desired response will he return to Shipping class again with
the help of interface. Another point to take home is the “division of responsibilities”,
can you image one class doing everything from taking orders to managing
inventory to shipping and billing etc.? that would be nightmare right?
So the conclusion is by using proper Encapsulation when we design
our classes, we can keep things nice clean and responsible. Let me know what you think, I hope to keep
the language as simple as possible, however, if you think “Spoon feed me now
and chew it too?” what can I say, on serious note, hope it helps J
Comments are always welcome!