What Is Object-Oriented Design?

   

To truly understand object-oriented design, you must look at what is at the center of this way of designing. You guessed it the object.

What is an object, you ask? Are they similar to objects we use, see, and touch every day? Yes! They are just like that. Objects can be exactly paralleled to physical objects we deal with every day.

Imagine you and I were on the phone and you bought a new ball recently at the store. I ask you to tell me about this new ball. Now you are in a position to explain conceptually something that is physical and apparent to you. You begin to tell me about its shape, color, texture, and so on. You are using attributes of the ball to give me a conceptual description of that ball.

Then imagine I have no idea what a ball is and need you to explain we can do with the ball. You would tell me that you can roll, bounce, throw, and kick the ball.

Through this description of the ball's attributes and what it can do, I can formulate a conceptual picture of this object and what it can do. I can picture in my mind what it looks like and how it will react to my interaction with it.

I think web designers and right-brained individuals have a distinct advantage over a typical programmer when it comes to understanding these concepts. It requires a bit of imagination and even creative thinking to understand it. You see, an object is a concept when relating to programming. It's a conceptual representation of something, or a "thingy" as I've said before. The concept of object-oriented programming enables us to create and build more complex "thingies."

So looking at our ball we can agree that as an object it is made up of things that describe it and what we can do with it.

Things that describe it:

Shape

Color

Texture

What we can do with it:

Roll

Bounce

Throw

Kick

From a programming standpoint there are proper names for these descriptions. The things that describe an object's attributes are the object's properties. What we can do with an object are the object's methods.

Instances and Classes

When people talk about objects within the realm of programming it is important to understand that the word "object" could potentially have two meanings. There is a distinct difference between an object class and an object instance.

"Oh no, here we go again. We are being sucked into the tornado of programmer's terms with no way out and headed for the 'Land of Snooze.' Classes, Instances, Properties, and Methods, OH MY!! Auntie Em, I'm not in Kansas anymore. There's no place like home…There's no place like home… There's know pla…"

Let's try to alleviate any anxiety you may have about this and help you to understand that what we are talking about is common sense. You don't need to fear these terms or feel that this is too complex for you to understand. It is actually quite simple.

The other day I was playing with my daughter and I asked her if she wanted me to go into her room and get a ball. Now there is a toy chest full of balls but I wasn't specific with her about what one I would get for her.

Her toy chest is filled with instances of balls. There are all different colors, shapes, and textures of balls. There are blue, red, brown, green and all the other colors of the rainbow. There are foam, rubber, vinyl, and plastic balls tons of instances of balls.

Notice that although there are lots of different balls, they all seem to have certain things in common. They all have a common set of properties and if you think about it, they all have a common set of methods, as well. They all have a color, shape, and texture, and they can all be rolled, bounced, thrown, and kicked.

Let's use the word "template" to describe what a class is. A class is a template of common properties and methods across a group of objects. It is a conceptual representation of the properties and methods of an object. So if I said to my daughter "Do you want me to get a ball from your toy chest?" I am referring to the concept or "class" of a ball. Objects in the toy box that are based on the Ball class all have color, shape, and texture, and can be rolled, bounced, thrown, and kicked.

After I retrieve a ball, I am bringing her a specific "instance" of a ball. It has a defined value for these different properties.

Methods are common among all instances of objects, so there aren't really differing methods among object instances. These are constant across the whole group, as shown in Figure 2.1.

Figure 2.1. Classes describe the properties and methods of an object. An instance is a named version with specific property values describing the instance's individuality.
graphics/02fig01.gif

As shown in Figure 2.1, the Ball class is made up of the color, shape, and texture property and includes several methods of using a ball object. The Roll(), Bounce(), Throw(), and Kick() methods are available to all ball objects.

Note

The parentheses are containers for variables that are passed to the method. So, for instance, if the Kick() method had variable strengths of how hard we kick the ball, we could pass this information through this variable, as in Kick("hard") or Kick("soft").


objMyBall has values that describe the properties as a color of white, a shape of round, and a texture of smooth. objYourBall has its own property values that describe it, as well. But this doesn't make our objects uniquely identifiable. It is our objects' names that make them unique, and in programming it is important actually required that instances of objects have unique names. How else is ASP.NET going to know which object we are talking about?

The diagram has two unique balls, objMyBall and objYourBall. The way to create new instances of objects is to assign names to the objects through variable creation. Variables are simply containers. They can hold objects and values, as well. You can name a variable anything you want, with the exception of reserved words such as "integer," "object," and "new," but it is good practice to use a uniform system when naming variables. In the diagram and for the ball, I used "obj" as a prefix to help you identify these as objects. The reason behind this is if I create a bunch of objects with such names in my ASP.NET pages, they will be easy to locate because they will all contain this prefix.

Creating Instances of Objects

How do you create a new ball object? First you have to assume that the Ball class is a part of the .NET Framework, which it isn't. If you can allow this stretching of reality, then you can declare a ball in this fashion in ASP.NET.

Warning

In the next few sections, I will display code examples that will throw errors if you try to plug them into a page and run them. This is because I am using an object that isn't native to ASP.NET, but one that you will discover how to create later, and if you refer to Appendix B, "Compiling Custom Objects," you will see how to turn this class into a full-fledged object that can be used in your applications. After you learn how to build your own class later in this chapter, you can use these examples as references.


Visual Basic .NET
dim objMyBall as Ball  dim objYourBall as Ball  objMyBall = new Ball()  objYourBall = new Ball() 
C#
Ball objMyBall;  Ball objYourBall;  objMyBall = new Ball();  objYourBall = new Ball(); 

Note

Please feel free to get excited. You now understand something that a programmer has a big fancy word for again and you didn't even know it. The operation of creating an instance of an object is called a "Constructor." Now I wonder whether you can guess the big programmer word for the operation of destroying an object. You guessed it. A "Destructor." We will discuss this a bit later.


We now have two ball objects with the name objMyBall and objYourBall available to use, define, and interact with in your ASP.NET application.

You may be asking "Why do I have to create a variable as a ball and then create a New Ball() after that?" In the initial line I'm just creating a variable that will contain an object and telling the variable what shape it is or in other words, what the variable will hold. ASP.NET doesn't actually make the ball until we stick it in the container. The variable name is the label on the container. You use the New() constructor to fill the container with the Ball.

To consolidate the function of creating like variables (containers), ASP.NET allows you to perform declarations of like object types on one line like this:

Visual Basic .NET
Dim objMyBall,objYourBall as Ball 
C#
Ball objMyBall,objYourBall; 

You still need to fill those variables with a Ball object by using the New() constructor.

Visual Basic .NET
Dim objMyBall,objYourBall as Ball  objMyball = new Ball()  objYourBall = new Ball() 
C#
Ball objMyBall,objYourBall;  objMyball = new Ball()  objYourBall = new Ball() 

ASP.NET has also provided a shortcut for this by enabling you to do both functions on a single line for each individual object.

Visual Basic .NET
Dim objMyBall as New Ball()  Dim objYourBall as New Ball() 
C#
Ball objMyball = new Ball();  Ball objYourBall = new Ball(); 

This shortcut makes it possible to create the variable and construct the object all in one shot for each object. No method is really better than the other. This is pretty much a personal preference issue, so feel free to use the method that you are most comfortable with.

Objects are pretty cool, huh? You bet they are! ASP.NET has hundreds of pre-built object classes to create and interact with in your applications. You can also create objects of your own like the ball. ASP.NET basically treats everything as an object, and you will see as the examples develop function and form that we are also creating objects to interact with. Kinda like building a log cabin with Play-Doh: If you need another log you just grab some more Play-Doh and roll it between your palms. If you need another object in ASP.NET, you massage your code to build another object in the application. We will do this later, but first you must understand some other stuff, so you get the whole thing under control.


   
Top


ASP. NET for Web Designers
ASP.NET for Web Designers
ISBN: 073571262X
EAN: 2147483647
Year: 2005
Pages: 94
Authors: Peter Ladka

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