Designing a Program Design


Assume that you are writing a class that models a kitchen oven. You think about it for a while, and you come up with a description, as depicted in the Unified Modeling Language (UML) class diagram shown in Figure 16.1.

Figure 16.1. A UML class diagram for the COven class.

graphics/16fig01.gif

The member variables shown in the UML class diagram might be interpreted in the following manner:

  • mOvenStatus ” Is the oven on or off?

  • mTemperature ” What is the temperature in the oven?

  • mDoorStatus ” Is the oven door open or closed?

  • mOvenLight ” Is the oven light on or off?

At this point the oven has four member variables. Although you could add other member variables to the class (for example, an oven might have a sensor you can insert into a roast to read its temperature), this is good enough for a starting point. In the COven class, you might want to alter the state of each one of these member variables through the user interface. You can do that with a property procedure. If you write property procedures for member variables, those member variables become properties that are accessible to users of the COven class. You have also added four class methods that can be used with the class. Although you might not have thought of everything, this design is a good starting point for the example.

Now you need to create a new project and name it Oven . Next , you should create a new class for the project by selecting Project, Add Class, and name the new project COven (see Figure 16.2).

Figure 16.2. Adding the COven class to a project.

graphics/16fig02.jpg

After you press Enter, the screen changes so that the new class is shown in the Code window. (Your screen should look very similar to the screen shown in Figure 15.3 in Chapter 15.) Now you need to enter the following lines of code in the COven Code window:

 Private mOvenStatus As Integer   ' Is the oven on or off  Private mTemperature As Integer  ' Oven temperature Private mDoorStatus As Integer   ' Is oven door open or closed Private mOvenLight As Integer   ' Is oven light on or off Public Property OvenStatus() As Integer 

These lines define the properties with the Private access specifier . As you know, this means that these class member variables are not directly available outside the COven class; that is, their scope is limited to the COven class itself.

Property Accessor Methods

When you enter the last line shown in the code fragment in the preceding section and press the Enter key, some pretty neat stuff happens. Immediately, new lines of code are automatically supplied for you by Visual Basic .NET. You should see the following in your Code window:

 Public Property OvenStatus() As Integer   Get  End Get  Set(ByVal Value As Integer)  End Set End Property 

By default, every property of a class has a property Get and a property Set method. To distinguish these methods from other class methods, the Get and Set methods are formally called accessor methods. As you have probably guessed, the accessor methods are used to change a property value (the Set accessor method) or to read a property value (the Get accessor method). Visual Basic .NET automatically supplies a code skeleton for each of these accessor methods. Of course, you must flesh out the details for the accessor methods.

Listing 16.1 shows the code for the OvenStatus accessor method.

Listing 16.1 The Code for the OvenStatus Accessor Method
 Public Property OvenStatus() As Integer  ' Purpose: To get the current status of the oven.  '      0 = oven off, non-zero = oven on  Get   Return mOvenStatus  End Get  ' Purpose: To change the status of the oven. It  '      is assumed that Value is the desired  '      temperature. Value = 0 means the oven  '      is off. Any other value indicates a  '      desired temperature value.  Set(ByVal Value As Integer) ' Turn oven on or off   Dim Flag As Integer   If Value <> OVENOFF Then    mOvenStatus = OVENON    ' Oven is now on   Else    mOvenStatus = OVENOFF  ' Oven is now off   End If   Flag = Bake(Value)  End Set End Property 

The Get method in Listing 16.1 simply returns the symbolic constant ( OVENON or OVENOFF ) for the mOvenStatus class member, depending on its current state. (From now on, I drop accessor from the term accessor method. If you see the terms Get method and Set method, you should understand that I am talking about accessor methods.) By having a Get method for mOvenStatus and keeping it Private , you are forced to use the interface of the COven class to change mOvenStatus . Again, using class Get and Set methods to read or write a class property reinforces the idea of encapsulation for the class.

The Set method for mOvenStatus is rather involved because you need to change the value of mOvenStatus . Note that the Set method has a parameter passed to it. The assumption is that a value of for the parameter reflects an oven-off state for the object, whereas a nonzero value says the oven is on. Again, you have defined symbolic constants for these values. (The complete code is presented in the following section, in Listing 16.2.)

Programmer's Tip

graphics/tip_icon.gif

A Set method can have only one parameter passed to it. Trying to pass more than one parameter causes an error message to appear.


The COven Class Code

Listing 16.2 shows the complete code for the COven class. Most of the code should look pretty familiar to you by now.

Listing 16.2 Code for the COven Class
 Option Strict On Public Class COven  '========== Constants =================  Private Const DOOROPEN = True    ' Oven door open  Private Const DOORCLOSED = 0    ' Oven door closed  Private Const OVENON = True     ' Oven on  Private Const OVENOFF = 0      ' Oven off  Private Const LIGHTON = True    ' Oven light on  Private Const LIGHTOFF = 0     ' Oven light off  Private Const BROILTEMP = 550    ' Broiler temperature  Private Const CLEANTEMP = 600    ' Self-clean temperature  '========== class member data ==========  Private mOvenStatus As Integer   ' Is the oven on or off  Private mTemperature As Integer   ' Oven temperature  Private mDoorStatus As Integer   ' Is oven door open or closed  Private mOvenLight As Integer    ' Is oven light on or off  '========== Constructor ================  Public Sub New()   ' Purpose: To initialize the variables to their proper values.   '      Even though some are zero, we set them explicitly.   mOvenStatus = OVENOFF    ' Oven is off   mTemperature = 0      ' No temperature   mDoorStatus = DOORCLOSED  ' Oven door is closed   mOvenLight = LIGHTOFF    ' Oven light is off  End Sub  '================ Properties ===========  Public Property OvenStatus() As Integer   ' Purpose: To get the current status of the oven.   '      0 = oven off, 1 = oven on   Get    Return mOvenStatus   End Get   ' Purpose: To change the status of the oven. It   '      is assumed that Value is the desired   '      temperature. Value = 0 means the oven   '      is off. Any other value indicates a   '      desired temperature value.   Set(ByVal Value As Integer) ' Turn oven on or off    Dim Flag As Integer    If Value <> OVENOFF Then     mOvenStatus = OVENON      ' Oven is now on    Else     mOvenStatus = OVENOFF   ' Oven is now off    End If    Flag = Bake(Value)   End Set  End Property  Public Property DoorStatus() As Integer   ' Purpose: This property gets the status of the door   Get    Return mDoorStatus   End Get   ' Purpose: This property sets the status of the door   Set(ByVal Value As Integer)    If Value = 0 Then     mDoorStatus = DOORCLOSED    Else     mDoorStatus = DOOROPEN    End If   End Set  End Property  Public Property LightStatus() As Integer   ' Purpose: This property gets the status of the light   Get    Return mOvenLight   End Get   ' Purpose: This property sets the status of the light   Set(ByVal Value As Integer)    If Value = 0 Then     mOvenLight = LIGHTOFF    Else     mOvenLight = LIGHTON    End If   End Set  End Property  Public Property Temperature() As Integer   ' Purpose: This property gets the oven temperature   Get    Return mTemperature   End Get   ' Purpose: This property changes the oven temperature   Set(ByVal Value As Integer)    Dim Flag As Integer    If Value < 0 Then     mTemperature = 0    End If    Flag = Bake(Value)   ' Set the oven temperature   End Set  End Property  '================ Methods ==============  Public Function Bake(ByVal Temp As Integer) As Integer   ' Purpose: This method is used to set the oven temperature   '      to the temperature passed to this method.   '   ' Argument list:   '  Temp   the desired temperature   '   ' Return value:   '  integer  1 if everything is OK, 0 on error   '   If mOvenStatus = OVENOFF Then  ' Oven is now off    Return 0           ' Error   End If   mTemperature = Temp    ' Since oven is on, set temperature   Return 1         ' Everything's OK  End Function  Public Function Preheat(ByVal Temp As Integer) As Integer   ' Purpose: This method is used to preheat the oven   '      temperature to the value passed to this method.   '   ' Argument list:   '  temp   the desired temperature   '   ' Return value:   '  integer  1 if everything is OK, 0 on error   '   Return Bake(Temp)  End Function  Public Sub Broil()   ' Purpose: This method is used to turn on the broiler   '      in the oven .   '   ' Argument list:   '  N/A   '   ' Return value:   '  N/A   '   mTemperature = BROILTEMP  End Sub  Public Sub SelfClean()   ' Purpose: This method is used to self-clean the oven.   '   ' Argument list:   '  N/A   '   ' Return value:   '  N/A   '   mTemperature = CLEANTEMP  End Sub End Class 

The code in Listing 16.2 begins with the definition of a series of symbolic constants. Next, it defines the member variables ”that is, the properties ”for the class. The access specifier for each of the member variables is Private . This forces the user of the class to use the class interface to change the state of the object.

Programmer's Tip

graphics/tip_icon.gif

There's nothing etched in stone about the code layout for class code. I prefer to place any symbolic constants first, followed by the member data. I place the constructor code next, followed by the properties, with the methods toward the end of the code. I think it helps to rope off each major section with a comment to make it easy to find the major code sections.

You are free to use whatever sequence you prefer. However, once you adopt a class code layout style, you should stick with it. This is especially true if you are part of a programming team. The coding style you select should also address the style for naming classes and their variables (for example, mOvenStatus versus OvenStatus ), symbolic constants (for example, use all uppercase or lowercase letters ), and procedure comments. Adopting a consistent coding practice makes it easier to test and debug code because it gives you at least an approximate idea of where to look for each code section. This is true even if you are writing code by yourself and not as part of a programming team.




Visual Basic .NET. Primer Plus
Visual Basic .NET Primer Plus
ISBN: 0672324857
EAN: 2147483647
Year: 2003
Pages: 238
Authors: Jack Purdum

Similar book on Amazon

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