Object Oriented Programming: Module Road Map
Object Oriented Programming: Module Road Map
This lecture is an introduction to a range of concepts and terms related to object oriented programming. This will not be an exhaustive lesson in OO. There are certain aspects I want to highlight in order to complete the material in the module. One of the things you will have noticed in the lab session for this week is that a lot of words were thrown at you. In understanding OO concepts there is a new vocabulary that needs to be understood in order to come to terms with the subject.
Within the module road map we are going to concern ourselves with the nature of custom classes in C sharp.
Credit Categories
This lecture will start to address the following credit categories. Assignment 1 Credit Category 3 Assignment 2 Credit Category 3 Assignment 3 Credit Category 2 & 3
These are the controls from Windows Media Centre allowing us to do the following Stop a track Rewind Skip previous Skip next Fast forward What I plan to do is use these controls to make a number of points related to object oriented programming. It is interesting to consider the history of these symbols and where they first originated. The technology in question dates back to the reel to reel tape recorders first appearing in the 1930s
This is a picture of a German Magnetophon machine. We can see how the technology works we have two reels. The first reel is loaded with magnetic tape and it transfers from one reel to the other as the reels spin. At the same time the tape passes over a play/recording head which translates the arrangement of the magnetic particles to sound. If we look at the controls on the machine we can see an early version of the controls we are interested in.
The recording / playback mechanism is very much dictating the controls on the interface. I have no idea who came up with the controls as we know then today or even when. However this vintage Polish recorder already has an early version of the symbols.
This common interface eventually became the de facto standard. (A standard that becomes popular because most people use/like it.) Over time what happened was that the same set of controls became used on a range of other products. Video Cassette Players
CD Players
MP3 Players
(It is curious because the reel to reel mechanism is no longer working behind the scenes!)
If we were going to buy a CD player we might look at Amazon and pick one there. We would look at its picture, examine the specifications and see the kind of thing that we are getting.
Now an advert in Amazon for a CD player is not a CD player. So we go ahead and buy the item and it is delivered. When it arrives we have an instance of the player as an object. The class is the definition (recipe) for an object The objects is the thing based on the recipe (the thing we use) When we create an object it is called instantiation (we create an instance of a class)
Remember a class is not an object, simply the definition of one. To do anything with this class we need to instantiate an object based on it. To do this in C sharp we would do the following: clsCDPlayer MyPlayer = new clsCDPlayer(); clsCDPlayer defines the name of the class file we are basing the object on MyPlayer is the name of the object we are creating = new clsCDPlayer(); ...makes a call to the constructor.
Constructors
The constructor is a section of code within the class that runs as soon as we create the object. It is rather like when we turn on our new CD player. It initialises the hardware, checks to see if there is already a disk in the tray and displays information on the liquid crystal display. In C sharp the constructor is a function with the same name as the class file...
Constructors may accept parameters allowing us to specify data at the creation of the object. For example... clsCDPlayer MyPlayer = new clsCDPlayer("track 11"); ...allows us to initialise the object and make it start at a specific track. Constructors may also be overloaded so that there are a range of initialisation options available.
Once we have instantiated an object based on the class we may now start doing things to it via its methods and properties.
To achieve this we use the objects methods and properties. Methods allow us to tell the object to perform a task Properties allow us to set or find out about the state of the object
For our CD player there are some very obvious methods and properties to consider. Methods Play Stop Pause FF Rewind Skip back Skip ahead Properties Track number Volume
Methods in C Sharp
A method in C Sharp is a defined as a function within the class. It may be void or return a value and may accept parameters. When the function is created in the class it is seen as a method by code using the class. For example we might have a Play method in the CD player class. The code within the class would look like this...
The code that creates an object based on the class might look like this...
We could modify the code such that it allows a parameter specifying a track.
In which case the code using the class might now look like this...
We could also change the return value of the function so that it returns a value of true or false based on if the track exists or not. The function within the class would look like this...
The code using the class might now look like this...
Properties in C Sharp
A property allows us to find out about or set something about the state of an object. One property that our class might have is Volume. The Volume property would be defined within the class like so...
There are quite a few things to notice here. Firstly the variable where the data is stored within the class is set to private. This is an example of data hiding. The idea is that we want any code using the class to only have access to the data we allow. To change the data it must use methods and properties that control (restrict) access. The private member variable is named using camel case.
For example volume firstName lastName Public Properties Named Using Pascal Notation Fore example Volume FirstName LastName Also notice the two parts of the property definition. The getter allows data to be retrieved from the member variable. The setter allows data to be sent to the member variable. The code using the class might look something like this...
We may find out the volume of the CD player by reading the value of the Volume property.
Encapsulation
Another term that you will come across in OO programming is encapsulation. It really isnt that difficult a concept to grasp. If we think back to the discussion on the controls on a tape deck we can see an example of encapsulation. For each successive device reel to reel, VCR, CD, DVD etc., the controls have remained the same. However the internal technology we are controlling has changed. This is even to the point where the technology on a computer based media player has nothing to do with the nature of the controls used. (On an MP3 player there is no tape!) What has happened here is that the external interface on the object has remained the same but the inner workings of the object has changed. Imagine with our CD player class we wanted to use a variation on the class to control a reel to reel tape machine. What would we need to do? We would start by re writing the code that controls the underlying hardware What we would not change are the methods and properties used by any code that makes use of the class
This allows our applications to be both modular and able to cope with change.
The data conduit class is a good example of encapsulation. The class provided on the module web site allows us to connect to an SQL server database using the SQL server specific ADO.NET (Active Data Objects) classes from the .NET class library. We are not going to open up the class in this lecture but we will take a first look at the interface provided for command and control. //create a new instance of the data conduit called Addresses clsDataConduit Addresses = new clsDataConduit(); //connect to the stored procedure Addresses.Execute("sproc_tblAddress_GetAll"); The first line of code creates an instance of the class called Addresses. The second executes a stored procedure called sproc_tblAddress_GetAll Let us suppose we wanted to take off the SQL server database and replace it with an OLEDB (Object Linking and Embedding, Database)? OLEDB is an alternate set of database drivers provided by Microsoft to connect to a wide range of other database management systems (e.g. Access, MySQL). To do this what we would need to do is change the code in the class but keep the interface the same. One issue worth thinking about at this point is how to address the problem created in our code by not encapsulating the name of the stored procedure. One approach would be to encapsulate stored procedure names in a Globals class. clsGlobals Globals = new clsGlobals(); clsDataConduit Addresses = new clsDataConduit (); Addresses.Execute(Globals.sproctblAddressGetAll);
The next step is to run the program. Next we enter some data and press save...
In order to step over lines at this level of the application press F10 F11 allows you to step into the classes in the middle tier.