Friday, March 18, 2011

Object Oriented Programming dalam VB

What is Object Oriented Programming?

Object Oriented Programming allows developers to mimic real world items and concepts. In an OOP world, each object knows what information describes itself and what actions it can perform with that information. OOP allows developers to create reusable objects that interact with each other to form complex entities.

Definition of Classes
A "Class" is a template which defines the attributes (properties in VB) and the actions or methods which can be performed upon those attributes for a given type of entity. The class definition is used to create "Instances" of the class, which are the actual objects used within your application.

A Class is generally considered to be a "Black Box". That is, you can use the object created from a Class without knowing how the internal code works. All the developer needs to know is which methods to call to perform certain functions. How those functions perform their tasks is hidden.

Programmers generally create a Class for each separate entity used within their application. Classes allow developers to write modifiable, readable, reusable code.

Objects = Attributes + Actions
An object is an "Instance" of a Class. In Visual Basic you can create any number of "Object Variables" from a single Class Definition. Each object knows what data describes it and how to manipluate that data.

Each data item is called an "Attribute" in OOP. Visual Basic implements these attributes as "Properties". Properties are exposed through the Public Interface of an object. Each object can have internal variables, not seen by users, which are used to store information the object requires to perform its task(s).

Example of An Object Which Exposes Public Properties

VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "clsPerson"
Attribute VB_Creatable = False
Attribute VB_Exposed = False

'Internal Member Variables
Private mFirstName as String

'Public Properties

Public Property Let FirstName ( Value as String)
'assign Value to internal variable from the outside world

mFirstName = Value

End Property

Public Property Get FirstName ( ) as String
'retreive internal variable and expose to outside world

FirstName = mFirstName

End Property

Objects not only contain their own data but they know how to perform tasks on that data. Tasks are performed by various functions and procedures defined in the Class Definition.These functions and procedures are called Methods in OOP. Once again only those Methods exposed by the Public Interface are available to outside users.

Example of An Object Which Exposes Public Methods

VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "clsPerson"
Attribute VB_Creatable = False
Attribute VB_Exposed = False

'Internal Member Variables
Private mFirstName as String
Private mLastName as String

'Public Properties

Public Property Let FirstName ( Value as String)
'assign Value to internal variable from the outside world

mFirstName = Value

End Property

Public Property Get FirstName ( ) as String
'retreive internal variable and expose to outside world

FirstName = mFirstName

End Property

Public Property Let LastName ( Value as String)
'assign Value to internal variable from the outside world

mLastName = Value

End Property

Public Property Get LastName ( ) as String
'retreive internal variable and expose to outside world

LastName = mLastName

End Property

'Public Methods

Public Function FullName ( ) as String
'retreives the persons full name

FullName = mFirstName & " " & mLastName

End Function

Encapsulation

One of the fundamental concepts of OOP is the method with which objects store and manipulate data known as Encapsulation.

Each object consists of internal variables which, when exposed to outside world, become its' attributes. That is, the data that is input and output from the object. VB defines these exposed attributes as Properties.

In addition to knowing what data to deal with, each object knows how to manipulate that data. Functions and Procedures that perform tasks are called Methods. A Method can be used internally or exposed to the programmer. If a Method is declared as Private then it can only be used inside the object. The outside world does not see it.

The developer communicates and controls the object via the Public Interface, a set of Public Properties and Methods which the object exposes to the outside. How an object performs its tasks is of no concern to the developer using the object. It is essentially a "Black Box".

Inheritance

In the pure world of OOP, objects can be created from other objects. Any properties and methods from one class are then automatically available as properties and methods of the new class. This is called Inheritance.

Visual Basic provides a modified version of Inheritance commonly termed Containment. A class can still expose its childrens' properties and methods but the developer of the class must explicitly reference the child in any calls made to those properties or methods. A comparison is the best way of explaining this concept.

C++ directly uses Person.Name when Student.Name is called





VB must explicitly call Person.Name when Student.Name is called

Let's look at how you would code this :

'Class : clsStudent
' Contains a reference to a Person Object

'private member variables
Private oPerson as clsPerson
Private iGrade as Integer

Public Sub Create ( )
'load this object and create the person object

Set oPerson = New clsPerson

End Sub

Public Property Let Name ( sNewName as String)
'set the person objects Name Property

oPerson.Name = sNewName

End Property

Public Property Get Name ( ) as String
'retreive the person objects Name Property

Name = oPerson.Name

End Property


Polymorphism

Objects can be as complex as you desire to make them. Some objects provide the functionality of several different objects with similar behaviours. This is Polymorphism, the ability to change the behaviour of a class based upon certain criteria.

Lets look at an example :

Two geographic areas have different methods to calculate Sales Tax.

Region A calculates Sales Tax at a flat 5% of the Sales Total.

Region B calculates Sales Tax on a prorated basis - 6% up to $200, 4% for all sales over $200.

We can create an object class which performs the calculation for Region A, and another class for Region B. Then create a class which references both the previous objects. When you wish to calculate taxes just tell your Wrapper Class which region to use and the proper calculations are performed.

Here's the code for this example :


'Class : clsTaxRegionA

Const iTaxRate = .05

Public function CalcTaxes ( curTaxableValue as Currency) as Currency

CalcTaxes = iTaxRate * curTaxableValue

End Function
'Class : clsTaxRegionB

Const iTaxRate1 = .06
Const iTaxRate2 = .04

Public function CalcTaxes ( curTaxableValue as Currency) as Currency

If curTaxValue < 200 then

CalcTaxes = iTaxRate1 * curTaxableValue

else

CalcTaxes = iTaxRate2 * curTaxableValue

end if

End Function
'Class : clsSalesTax

'private member variables
Private sRegion as String
Private oRegionA as clsTaxRegionA
Private oRegionB as clsTaxRegionB

Public Property Let Region ( sRegionFlag as String )

sRegion = sRegionFlag

End Property

Public Sub Create ( )

Set oRegionA = New clsTaxRegionA
Set oRegionB = New clsTaxRegionB

End Sub

Public function CalcTaxes ( curTaxableValue as Currency) as Currency
'calculate taxes based on region

if sRegion = "A" then

CalcTaxes = oRegionA.CalcTaxes (curTaxableValue)

else

CalcTaxes = oRegionB.CalcTaxes (curTaxableValue)

end if

End Function



No comments:

Post a Comment