Most modern software can be controlled programmatically
through its exposed object models. In fact, most of the
applications you use everyday on your Windows PC have an object model, including
Microsoft Word, Excel, and Internet Explorer.
The careful design of these object models is the key to building good software.
We're now going to embark on the design, and start the construction, of the business
tier for Jo's Coffee. This business tier will consist of a number of objects whose
interrelationships are defined by their position in the object model. Implementing
the object model will define what we can do within the application. During the
course of the chapter we'll be discussing the object-oriented approach to
programming as well as ActiveX and COM technology; all topics that directly influence
the way we construct the application.
Note: These topics merit whole books on themselves, and we are merely
going to provide the background you need for our project. If you wish to know more
try Beginning Visual Basic 6 Objects,
ISBN 186100172x and VB COM, ISBN 1861002130.
Both by Wrox Press
There are going to be three basic parts to this chapter –
we're going to start off with the theory (concepts behind object-oriented
programming, COM and ActiveX) then we'll move onto see how that theory
influences our design of the object model for Jo's Coffee. Finally we'll begin
to implement the design in the WroxCommerce VB project.
More specifically our path through the chapter will
involve:
- q Looking at object-oriented programming
q Understanding a little about components
- q Designing an object model for Jo's Coffee
- q Creating a project in Visual Basic
q Building the Visit object
- q Building the Database object
- Building the Catalog
object
Let's start by looking at object-oriented programming.
Object-Oriented Programming
Object-oriented programming (OOP) is based on the idea that
real-world entities or relationships can be represented in code as objects.
These code objects have associated with them data and behave in certain ways when
asked to. Objects can be linked together to form programs and applications.
Objects may be regarded as
black-boxes – the user of an object may know what information needs to be
input to the object to get a certain result out, but does not need to know what
process goes on inside the object.
OK, let's step back from that rather terse summary and use
the metaphor of a television set to explain this concept. We're all pretty
conversant with televisions in so much as we know if we input a signal (from
aerial, cable, video or wherever) and we use the control set properly we'll get
sound and vision. Pushing the buttons allows us to vary the channel we watch,
raise or lower the volume, alter the picture brightness, and so on. Most of us
don't know the precise workings of the electronics that control the TV, and
neither do we care, because we know enough to be able to control it.
Object Behavior
In the first paragraph we said that objects contain data and
have behavior; in programming data is represented as variables while behavior is represented
by terms familiar to all VB programmers;
properties, methods and events. So to continue our TV analogy
we could have a property called
channel which you could query to discover what channel was being displayed, or
alternatively you could set that property to actually change the channel. Note,
a property is different to the underlying data – in the case of the channel
property the underlying variable may be the actual frequency of the input signal
that gives the picture corresponding to that channel number. Methods describe
something the object can do, so our television may have IncrementVolumeOneStep
or SwitchToVideo
methods that are invoked in response to input from the control set. Events occur
when an action or state occurs, so the event of pushing the Increase
Volume button would fire the IncrementVolumeOneStep
method.
Incidentally, the process of objects hiding their internal
design and data from the outside world is termed encapsulation. It means an object can
hide data and not allow it to be accessed. So on a TV there maybe a TVMustNotBeOnForLongerThan
property that controls the maximum length of time a TV can be on before it
automatically turns off to ensure it doesn't overheat.
An Object Example
The properties, methods, and events an object has form the interface through which that object can
be manipulated.
All this is very familiar in VB where a control like a CommandButton
is an object and has, for example, a Caption
property and a Click
event to which you can attach a method. It acts as a black-box, as you the
programmer do not have to worry how the button responds to the click input or
how it looks – that has been sorted out for you.
Classes
A class is basically a template from which we stamp out objects (like a biscuit
punch) with each created object being termed an instance of the class and the process
of creating the object being termed instantiation. In Visual Basic, classes
are defined using class modules (in C++, they are simply
known as a class).
An Object Example
OOP in Action
The vast majority of
object-oriented programming work is concerned with building objects that have no
user interface. These objects provide some function to an application. Imagine
you are working on a development project in a team and you need access to a
billing system that your company owns. One of your colleagues has built a set of
objects for talking to this billing system, but it is used on another
application. In an object-oriented world, if your colleague has done their job
properly, they can give you a copy of the objects, you can plug them into your
application and now your application code has access to a billing system through
those objects. You haven't had to learn anything about how the billing system
works; you've just plugged the objects in.
Let's say our colleague gives us their class module called
BillingSystem,
(and provides us with information about its properties, methods, and events) in
VB we can create instances of it like this:
Dim MyBillingSystem
Set MyBillingSystem = New BillingSystem
Once we've created a billing system object, we can use it:
MyBillingSystem.BillCustomer "Neil",
"Mcevoy", "$500", "1/27/2000"