Thursday, May 8, 2014

Object Oriented Development for Non-Programmers: Part 1 - What is An Object?


I was recently asked by a designer on our team to help her understand programming so she could better communicate with her team. I am in the process of coming up with a series that will assist her in understanding the concepts of programming and object oriented design without being overly technical.

I develop regularly in C# so any examples will be in C#. Most modern languages are object oriented so the concepts can be applied to other languages, such as C++ and Java.

Keywords are underlined.

What Is An Object?

The first role of a developer is to design how we are going to model the data we are working with. We need to determine what data to model and how it is going to interact with the other data. This is our program architecture.

With object oriented programming, we model our data much like we interact with the world.

For example, right now you may be sitting in a room. A room is an object. A room has attributes such as width, height, and depth. In programming, we would call those properties.

In our code, we would create an object called Room with properties Width, Height, Depth. So in our code, we have a model for what makes up a Room object. At this point we have only modeled room, we do not currently have any rooms. We have a concept of what a room is - essentially a room is a width, depth, and height. Therefore an object can be described as a collection of properties.

To have a room in code with properties, we need to create an instance of the room. Creating an instance says to the computer, "Hey, allocate a block of memory to fit one 'Room' object." Once we have an instance, we can set the properties.

The code for our example is below. Note that this is C# code, but not a full implementation. Some has been removed to simplify and avoid confusion while we focus on what an object is and how we instantiate (create an instance of) it.

This is the code for our Room object. We define the name of the object and the properties.
    class Room //  "Room" is the name of the object
        // Properties listed below:

        int Width; // the int signified "integer", or whole number

        int Height;

        int Depth;

This is how we instantiate Room objects and set the properties on the object. We use the "new" keyword to tell the computer to create a new instance and allocate the memory. We can create as many Room objects as we'd like.
            Room room1 = new Room(); // Create a new instance of a Room named "room1" - you can name it whatever you'd like.

            // Set the properties on room1:
            room1.Depth = 10;
            room1.Width = 20;
            room1.Height = 30;

            Room room2 = new Room(); // Create a new instance of a Room named "room2"

            // Set the properties on room2:
            room2.Depth = 15;
            room2.Width = 25;
            room2.Height = 35;