Encapsulation


Perhaps the most important of the object-oriented concepts is that of encapsulation. Encapsulation is the concept that an object should totally separate its interface from its implementation. All the data and implementation code for an object should be entirely hidden behind its interface. Another way to put this is that an object should be a black box.

The idea is that you can create an interface (by creating public methods in a class) and, as long as that interface remains consistent, the application can interact with your objects. This remains true even if you entirely rewrite the code within a given method. The interface is independent of the implementation.

Encapsulation enables you to hide the internal implementation details of a class. For example, the algorithm you use to find prime numbers might be proprietary. You can expose a simple API to the end user, but you hide all of the logic used in your algorithm by encapsulating it within your class.

This means that an object should completely contain any data it requires and should contain all the code required to manipulate that data. Programs should interact with an object through an interface, using the properties and methods of the object. Client code should never work directly with the data owned by the object.

Important 

Programs interact with objects by sending messages to the object that indicate which method or property they’d like to have invoked. These messages are generated by other objects or by external sources such as the user. The object reacts to these messages through methods or properties.

Visual Basic classes entirely hide their internal data and code, providing a well-established interface of properties and methods with the outside world.

Let’s look at an example. Add the following class to your project; the code defines its native interface:

  Public Class Encapsulation   Public Function DistanceTo(ByVal x As Single, ByVal y As Single) As Single   End Function   Public Property CurrentX() As Single     Get     End Get     Set(ByVal value As Single)      End Set   End Property   Public Property CurrentY() As Single     Get     End Get     Set(ByVal value As Single)     End Set   End Property End Class 

This creates an interface for the class. At this point, you can write client code to interact with the class, because from a client perspective all you care about is the interface. Bring up the designer for Form1 and add a button to the form, and then write the following code behind the button:

  Private Sub btnEncapsulation_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles btnEncapsulation.Click   Dim obj As New Encapsulation   MsgBox(obj.DistanceTo(10, 10))    End Sub 

Even though you have no actual code in the Encapsulation class, you can still write code to use that class because the interface is defined.

This is a powerful idea. It means you can rapidly create class interfaces against which other developers can create the UI or other parts of the application while you are still creating the implementation behind the interface.

From here, you could do virtually anything you like in terms of implementing the class. For example, you could use the values to calculate a direct distance:

 Imports System.Math Public Class Encapsulation    Private mX As Single    Private mY As Single     Public Function DistanceTo(ByVal x As Single, ByVal y As Single) As Single      Return CSng(Sqrt((x - mX) ^ 2 + (y - mY) ^ 2))     End Function     Public Property CurrentX() As Single     Get        Return mX     End Get     Set(ByVal value As Single)        mX = value     End Set   End Property   Public Property CurrentY() As Single     Get        Return mY     End Get     Set(ByVal value As Single)        mY = value     End Set   End Property End Class

Now when you run the application and click the button, you get a meaningful value as a result. Even better, however, encapsulation enables you to change the implementation without changing the interface. For example, you can change the distance calculation to find the distance between the points (assuming that no diagonal travel is allowed):

 Public Function DistanceTo(ByVal x As Single, ByVal y As Single) As Single      Return Abs(x - mX) + Abs(y - mY) End Function

This results in a different value being displayed when the program is run. You haven’t changed the interface of the class, so your working client program has no idea that you have switched from one implementation to the other. You have achieved a total change of behavior without any change to the client code. This is the essence of encapsulation.

Of course, a user might have a problem if you make such a change to your object. If applications were developed expecting the first set of behaviors, and then you changed to the second, there could be some interesting side effects. However, the key point is that the client programs would continue to function, even if the results were quite different from when you began.




Professional VB 2005 with. NET 3. 0
Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
ISBN: 0470124709
EAN: 2147483647
Year: 2004
Pages: 267

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net